Am I misusing Ruby on Rails Layouts - ruby

EDIT:
Okay, so using cells I have got it running, indeed the rb file created by the cells scripted needed:
helper ApplicationHelper
this got past the error in the comments below, but now inside my application helper, i have a method:
def hr_user_past
#current_year = current_year
#hr_user_past ||= Employee.where("username=? and year < ?", session[:hr_user].username, #current_year).order("Year DESC") if session[:hr_user]
#past_times = #current_year - 2011
#blart = 'test'
end
And alas, this now cannot find current_year which is a def method in my application_controller.rb, so I think my flow is all messed up and not sure what I need to have where for a good solid "include".
Def of the current_year from application controller
class ApplicationController < Actioncontroller::Base
helper_method :current_year
...
...
def current_year
OpenPeriod.maximum :year
end
end
I also am worried all this work still wont solve the issue I am having with the twitter bootstrap modal dialog being called from a layout and not fadeing in all the way with data (minute i put the same code in a view, it works right)... but one issue at a time i guess...
END EDIT
Trying to have an objective question here though. Some background:
I have a navigation layout that has this nice login, home button and a few others on it. It works fine, lately the customer wanted some more functionality added to this namely a smart button that drops down dynamically with data filled out (stuff from years past from old forms we use).
So the app used a twitter bootstrap modal elsewhere. I just posted this question here:
twitter bootstrap modal does not work in rails3 layout?
Where I am having an issue with this modal working correctly from the layout. Then I got to thinking, this modal that drops down has some good amount of data usually from a specific controller, but they want to see this data at any moment. Is the layout way of doing this not the way, should I build an application helper .rb code to fill out a partial and include that partial on every page in the app? ( seeems like it should fix my modal displaying oddly, like constantly faded issue). I think my green-ness to ruby on rails is at fault here. I know its awesome and there is a lot to it and this is the first project I have done in it. So I am not sure what the correct solution is here, and I think its a vocabulary issue. Like what am i looking for?
Form helpers
Templates
Partials
etc etc... what is the best fit here? I realize in coding there is a lot of ways to skin a cat but I am not sure where to ask. Rereading this is getting subjective. Basically
I have a home controller that sets up things on a home page, turns out this data needs to be seen by the user at any given moment from any page in the app. Tried to move it to our navigation layout, its not going well. Is this the right approach or is there a better one?

One approach would be to use Cells. Essentially, a cell is a miniature controller that takes care of fetching data and rendering a view. It's not quite a full-blown view, it's a component which, similar to a partial, may be included into other views of your application - or even the layout, which is useful when you have something that appears on every page.
A classic example (which they implement on their homepage) is that of a shopping cart - it appears throughout your app but it needs to have some data set; turning it into a cell extracts both the querying and the rendering into a nicely-separated MVC unit.

Related

What is the right method to add text areas to Django?

I have written python code that analyses two text sources and compares them.
I'd like to implement this dynamically via two text boxes that a user could either type into or manually upload. I have already begun coding this using HTML. Would it be better to implement a widget or the models instead to make the text area boxes?
Edit:
I wrote this question when I was just figuring Django out, so forgive me if it sounds confusing. But everyone starts somewhere. I'm unable to delete the question as contributions have been made already. YouTube courses proved helpful in learning the basics, if any beginners stumble upon this.
You use a form object. Django has form objects (https://docs.djangoproject.com/en/2.0/topics/forms) that take a model and translate it into html elements. So I guess in a way, you implement the model but I want to stress that that's not actually what's happening from a technical standpoint. A better way to say it is that you're implementing forms. The reason I stress this so much is so that you understand what's really going on so that you don't have misunderstandings that end up costing you in code clarity and readability.
So to answer your question, you can implement django forms to do this very easily. The way you implement it depends on your models and how they are designed since the forms use the models to create the right html form elements. If you're dealing with one model that will be instantiated by the form input, create a model form. This will take the input from the form and create your model instance. If you're dealing with one form that uses multiple models, then use a generic form. In this case, you will have to write your own save method that does the actual logic of the form.
One other thing to add... No matter what, your end result will always be a widget on the HTML in the end. Django forms translate a model class into a form element with input elements. If you didn't use Django forms, you would still do the translating, but you would have to do it from scratch.
I hope this helps and that I correctly understood your question.

a rails partial but used by multiple views

I have a piece of code that I would like to have show up on several areas... from two different views. Which is to say handled by two different controllers (all the data comes from a helper though) what is the best way to break this up, should these partials be rendered then as lay outs? Though they use the twitter bootstrap modal and I have had huge issues getting those to show correctly when called from a layout (most the reason why I have to break up this code into a partial, one that comes from one index.erb.html and another that comes from a partial of its own _document_header.erb.html good time to ask, can you stuff one partial within another...
so just trying to get some pointers. This code is really common to two views.
(and is not always needing to be rendered or displayed)...so want to do this the best way.
You can reference a partial that is anywhere in your views folder.
It's common to make a "shared" folder in your views and reference the partial like: <%= render 'shared/my_partial' %>

backbone.js load different html

i've been posting alot questions lately on stackoverflow due to my issues i'm facing recently and you guyz have realy helped me alot.
MY Question:
I'm working on backbone.js and i've worked on codeignitor on my project. Now my question is in ci, i can load views directly from ci view and can display dynamic html easily ( working fine for me). problem is i've to shift my js code and functions and ajax calls to backbone.js and i'm pretty much at the start of this project. i'm not clear on how can i show entirely new page html on some events in backbone.
my question Not clear? let me explain further.
Explanation:
i mean i've some an index page and all of things load in this page ( sliders , footers etc etc ). whenever any item is clicked to see the details, ci calls the appropriate methods and gives me the html which displays that product in an entirely new page ( without loading ) with only image of that product and some other options for the product. it's pretty easy with ci, but how can i do this with backbone.js to show up different pages or entirely different html from index page and not showing other stuff of index page.
i can work out how to move and design the logic behind it but i'm not getting the picture of how can i load these different html for different events ( like about button should show about us page, featured category should show featured products all in that main index file.)
I'm really desperate to get some directions.
thnx guyz
I think you were meant to say you want to make single page application.
Yes, backbone best idea to do so. You will required following :
template ( should be inside <script type="text/template"> and </script>)
backbone view
backbone model
backbone router

mvc 3 partials and usage

We meet again stackpeople!
I have been using the last 2 days trying to find the answer I need.I can't seem to find a straight forward answer on WHEN to use partialviews I know you can use em for like login component and all the other fancy stuff
.
But what about the navigation bar ? I tried making the navigationbar with partials and Ajax.htmlactionlink but then the problem comes, I know since its just a partial my URL won't get rewritten which means on f5 it will always update my home/index since no URL was given . Is this cus I can't make my navi like that or just because im plain stupid ?:)
Partials should be used to avoid code duplication. Create one if you find yourself writing the same view code over and over again.
A navigation bar sounds more like something that should be in the layout. You can use Sections in Razor and ContentPlaceHolder for the webforms view engine if you want to let pages customize the layout.
I don't think the partials have anything to do with it(it shouldn't, partials are just a way to split up source files so you can reuse them later).
I'm guessing the problem lies in the use of AjaxhtmlActionlinks, why would you want to do an Ajax call to redirect the user?
Try using the normal #Html.ActionLink()

Asp.Net MVC 3 - #Html.Action won't render/return any HTML

I've been moving a fairly new project from ViewPages to Razor today, and all seems to be going well. Except I'm trying to use Html.Action to render a user control and it won't render anything.
So I have a Shared/_Layout.cshtml file which is referenced in Home/Index.cshtml
Index.cshtml has the following:
<article>
#Html.Action("LatestBlogsMainPanelWidget", "Blogs")
...
</article>
I've put traps in the BlogsController, so I know that's being requested. I also know that a model is being returned, that the LatestBlogsMainPanelWidget is being found by the view engine, and even some dummy Razor syntax code is being run:
#{
var s = "hello";
}
but the plain html in this file isn't making it to the browser. I've tried other (previously working) partials too and they won't appear either (view source on the page confirms it's not there).
I've also tried substituting for
#{ Html.RenderAction(...); } without success. HTML either side of the #Html.Action is appearing, so I know Index.cshtml is displaying properly.
Even more strangely the _Layout file also has Html.Action commands and they do appear fine.
I'm really not sure what else to check, or how to confirm that the pipeline is getting the HTML. Can anyone help at all?
Thanks!
Put a Layout = null on the partial view and it will work fine.
Try this:
#{Html.RenderAction("LatestBlogsMainPanelWidget", "Blogs");}
The brain is a funny thing, and despite spending several hours on this yesterday, it took my dog waking me up in the middle of the night for a wee for my subconscious to stumble upon the answer.
If this had anyone else stumped, I'm not surprised. I hadn't mentioned because it hadn't dawned on me that I was using a partial-level caching system similar to one designed by Steve Sanderson. It suddenly struck me that this could be the cause, since to the best of my knowledge Razor pages go through far less pipeline processing than WebForm pages. The caching filter is probably not doing what it needs to do, or at the right time.
I've confirmed that commenting out the OutputCache filter on the Actions in question has fixed the problem.
I've no idea if this issue is true of the page-level caching as it's not something I find useful.
While searching solutions for this issue, I have find out three measure issues for not proper rendering of Html.Action and Html.RenderAction. Please verify have you done below things properly or not.
In your PartialView or View you have defined #{Layout = null;}.
Use return PartialView instead of View .
Decorate your action with [ChildActionOnly] attributes.
I hope by applying above all steps you can solve your issues.

Resources