Setting content_for misunderstanding - ruby-on-rails

I'm using this tutorial to set bootstrap modal content in forms, but I want to set the title...
I think I don't understand how to set the title content_for method...I understand it for templating, but if I want to send a title to the modal, is it best to make a helper for that? Is there a better Rails 4 way to do this?
Please advise, and remember, I'm not a CE--I've looked at the rails doc and google searched, so please don't demean my question by posting a google link. Thanks!

If you're going to be rendering the title of the modal dynamically, it's typically better to use a helper method. This will keep your view logic DRY and readable. This method will accept an argument for the title and if none is present it will render a default title.
In application_helper.rb:
def modal_title(title)
if title.empty?
"My Default Title"
else
title
end
end
In your modal partial where the title should appear:
<%= modal_title(yield(:title)) %>
In the view, you will have access to dynamically set the title using content_for. For example, you could pass a string as your title or pass an instance variable from your controller.
<% content_for :title, #your_variable %>

Related

Make specific Rails view show certain helpers

How do you make a specific view show a different helper.
module ApplicationHelper
def about_title
%Q{Everything you need!}.html_safe
end
def contact_title
%Q{Contact us!}.html_safe
end
end
I have a partial header that renders in every view but I want to show a different header title without having to edit the views directly (or edit a variable? in the view to show what I want?)
<h1 class="title"><%= about_title %></h1>
What I'm thinking is making an if/else in the helper but I am new to rubyonrails and I'm not sure how is this done.
You need to set the title somewhere. There's a few ways you can handle this. You could use the controller + action name, and set up the titles in i18n.
en:
titles:
home_index: "Welcome!"
session_new: "Login"
Then in your layout:
<title><%= t("titles.#{controller.controller_name}_#{controller.action_name}") %></title>
Another way to do it is using content_for & yield but that means putting code in your views :)

print line in rails only for a specific page

I am new in rails and have the following issue:
I have a view that renders from the layout across several pages, it is in fact a part of the header. I want to remove a specific line from the header and hide it only for one page. I have tried to add a condition in the view to check if the page rendered comes from a specific controller/action but it doesn't work. what is the best practice for this case?
thanks
Just keep a instance variable in controller action and set is as true. Use this variable to with conditions to include/exclude specific code.
controller action
#include = true
view
if #include.present?
//place the line which should be excluded
end
I've asked a very similar question, and you are allready on a good way:
Use a condition like
<% if current_page?(view_path)==false %>
<your line>foobar</your line>
<%end%>
In the view you can access the controller from params[:controller] so you could have
<% if params[:controller] == foo %>
<p>conditionally shown</p>
<% end %>
Its not always best practise to use params directly inside the view though...

New to RoR, confused about where to put markup

I'm very new to RoR, moving from PHP. I have what I hope is a simple question.
Say I have a Model called Article, stored in the table articles.
If I want to display a list of articles on several pages in my site, I might have some simple markup such as;
<% Article.find_each do |article| %>
<div class="article_list_wrapper">
<h1><%= article.title %></h1>
<p><%= article.body %></p>
</div>
<% end %>
Where would I put this markup? I mean in what file; if I put it in a view, then I will have to copy it if I want a similar list in a different part of the site.
Putting it in the Model itself seems intuitive to me, the Article object should know how to render itself, right? But that seems totally against the CMV idea.
And what if I wanted to have several ways of rendering the same object? Like a list view, a full page view etc.
Many thanks, just trying to get into the correct habits from the start.
You would put that portion of HTML markup into what is known as a partial -- something that can be rendered as a small piece of a larger layout. You're right, you wouldn't want to store the format in the model, because you might wish to render that specific HTML for web browsers and render very similar XML or JSON for clients using an API and render some other very similar content into ASN.1 or YAML for further other clients. The "view" really should stay associated with the views.
With this specific example, you would probably go one further and provide the articles to render via a variable rather than letting the view find the articles itself:
Controller:
def flubber:
#articles = Article.find_each(some_criteria)
end
View:
<div class="article_list_wrapper">
<%= render :partial => "article_list", :collection => #articles %>
</div>
And then provide a View for the corresponding controller with the name _article_list:
<h1><%= article.title %></h1>
<p><%= article.body %></p>
If the <div class="..."> is going to be identical in every single use of this partial, you could either pass a :template to the partial, or put it into a view helper, or put the entire render with the :template argument in a view helper. (Either the first or last approach here is what I'd do -- the <div class="..."> doesn't seem so important to require its own file, but if it were more complicated, it might -- and then I wouldn't want to give the partial :layout argument every time I needed it.)
MVC (Model-View-Controller):
The Model has (primarily) the database piece and business logic.
The Controller controls application flow.
The View shows the info, e.g. the browser view.
So the markup you show which has a bunch of HTML goes in the view.
That is app/models/views/article.rb
The Controller is the part that gets the info.
You should actually have #article = Article.all in your controller and then just use #article.each instead of Article.find_each in your view.
If you have conditions you can apply them directly on the Controller find but the better approach is to learn about how you can use model finders with scopes and then use those scopes from the controller. Always try and push things up from View to Controller to View and follow the 'fat Model, thin Controller' approach when practical.
For reuse partials can help but as better understanding of rails will help more.
See railscasts by Ryan Bates. Incredible tutorials.
You might also like my own bookmarks for rails at my (rails) linker app.
You can use partials for this.

Providing default content for section in layout(rails 3 .erb)

Is it possible to provide default content for section in layout if it is not overridden in view?
if you are using, say a title tag in your head section of your application.html.erb layout file, you could do something as simple as <%= #title || "Some Default App Content" %>
Using this same approach, you can use it for content_for and other helper methods.
Now, Rails have content_for? method for it: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

render all passed parameters quickly in rails

I have a form that populates various values, then posts those values to a page when the user submits. Now, I wish to see what params[] contains (by displaying it on the page I post to) just to fool around with my form definitions a little bit. I looked for an easy way to render params, but haven't quite found the solution. Any helpful suggestions SO?
thanks in advance
Another way is to make use of the helper functions for debugging as described in http://guides.rubyonrails.org/debugging_rails_applications.html
The technique, as previously described is to use params.to_yaml.
Alternatively, in your application.html.erb file put the code
<%= debug(params) if Rails.env.development? %>
after the <%= yield %> call
This will display in the view the params, for example
--- !map:ActiveSupport::HashWithIndifferentAccess
action: edit
controller: contracts
id: "8"
The nice feature is that the information is only output in the Development mode environment, as per the check that is executed to determine which Rails environment is running.
Try <%= params.to_yaml %> in your view
Try using in this view in the view you are posting
<%= params[:name_of_posted_param].each {|param|
params //do something with the param here
} %>
A better way would be to do this in the helper and pass the returning variable in the view.
-R

Resources