I have a link_to image_tag that when clicked should pop up a modal to show a help screen. The button code is:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_"{current_step} %>
where the {current_step} is the name of the step the user is on that corresponds to the help screen they are accessing.
I am getting a SyntaxError outputting the current_step.
Can someone help with the correct syntax?
Interpolation in strings in ruby looks like this: "#{var}", so you need to do this:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_#{current_step}" %>
Note that you have to use double quotes(") for string interpolation, if you use single quotes('), it won't interpolate.
Related
I am trying to build a dropdown select input of months using simple_form. However, I am having trouble figuring out where to even begin. Currently, it is a text input area:
<%= f.input :start_month %>
I need to know what arguments to pass in order for this to be a dropdown of all 12 months. It is not important for it to return an integer value for the months but would be ideal in the event I use it for ordering later on.
I am still a beginner with rails and could really use the help on this one. I can provide any extra information necessary.
Edit:
I would like the dropdown to show the month names, not simply numbers.
Here is my solution:
<%= f.input :start_month, collection: (1..12).map{|i| [I18n.t("date.month_names")[i], i]} %>
What about this:
<%= f.input :start_month, :collection => 1..12 %>
or with month names:
<%= f.input :start_month, :collection => ['January','February',...,'December'] %>
<%= f.select :month, Date::MONTHNAMES.compact.each_with_index.collect{|m,i| [m,i]}, prompt: 'Month'
I'm trying to create a very simple email signup form, where the user can just enter their email and press submit.
I'd like the signup form to be a partial, so I can use it anywhere on the site.
Forms are coded like this:
<%= form_for #subscription do |f| %>
How to do tell the form that #subscription is a Subscription.new if, say, the current view, controller, and action is the home page?
Thanks!
As an option you can do:
<%= form_for Subscription.new do |f| %>
One thing though, I haven't tried this myself. Try it and see if it works.
Edit:
Sorry, I didn't get the question in the first read.
You can set the #subscription instance variable to Subscription.new in the home controller, and then, pass it to the partial (inside the main view) like this:
<%= render partial, :subscription => #subscription %>
Hope this helps.
You'll have to make sure that #subscription is instantiated before calling the partial ... or do :
<%= form_for #subscription || Subscription.new do |f| %>
So if #subscription exists, it uses it. If not, it'll fall back to Subscription.new
You can declare #subscription as Subscription.new right in the home action
I am using devise and cancan with rails 3.2.1 and I'm having an unusual problem that I know can be refactored and simplified, but after much searching, I'm stuck.
I have first_name and last_name fields in the user model and have successfully displayed the full name of the user in the users/show.html.erb view using this code:
<%= #user.first_name + " " + #user.last_name %>
And I was successfully displaying a link to the user's show page in the header using:
<% if user_signed_in? %>
<li><%= link_to #user.first_name + " " + #user.last_name, current_user %></li>
<% end %>
But, and I can't figure this one out, after working on another part of the app, the header started giving me an undefined method error message...
In any case, I know there is a much better way to define and work with user names and I have tried some things out, such as adding a name method to the user model, but as you can clearly tell from this post, I am a beginner and have not been able to make that work.
Please let me know if you have any ideas on how to best work with users names in an application like this.
Where are you defining #user? I'm willing to bet that it's in your UsersController show action, which means it's defined for your header in that action only.
Try replacing your header code with this:
<% if user_signed_in? %>
<li><%= link_to current_user.first_name + " " + current_user.last_name, current_user %></li>
<% end %>
The trick is that Devise makes current_user always available when the user is signed in. You don't have to worry about setting a #user instance variable.
In my view I want to display some right double angle quotes in my link.
Before Rails 3, this worked:
<%= link_to "» #{#category.name}", some_path %>
Now what should I do if I want to specify the » as html_safe but not the rest of the link's text?
In other words I do not want to do this:
<%= link_to "» #{#category.name}".html_safe, some_path %>
I do not want the #category.name treated as html_safe.
This produces the desired result:
<%= link_to "»".html_safe + " #{#category.name}", some_path %>
However, if I do this:
<%= link_to "#{#category.name}" + "»".html_safe, some_path %>
The output of the angle quotes is not treated as safe. I see » on my page and not ».
Why?
I tried extracting "»".html_safe to a helper method with the same results.
Is there a way to easily designate hard coded text/symbols as HMTL safe in Rails 3?
Thanks.
In this situation I often explicitly escape the unsafe part:
"» #{h #category.name}".html_safe
you need to make sure that the whole string is html_safe...
I'd recommend to try this:
"» #{h #cagegory.name}".html_safe
I am running Rails 3 with the following code in the view
View:
<%= #found_docs.each do |doc| %>
<%= doc.id | doc.content %>
<% end %>
As a result I get two objects, as expected - but in addition a third result is displayed. It is created by calling super(value.to_s) on the result set. Doing #founds_doc.count returns 2 as expected.
Why is the third object displayed when running the block, when #found_docs has only two objects ?
I found the solution - and I did a silly mistake !
The view code is as follow:
<%= #found_docs.each do |doc| %>
I put the "=" sign in front of the loop, hence the result of the loop is printed with value.to_s in the view.
Correct code is:
<% #found_docs.each do |doc|%>
Must have been blind :-)