Rails how can I put <%= link_to ...%> into an instance variable? - ruby-on-rails-3.1

I have this (and other) navigation links:
<li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> |
These need to be put into the header part of my templates and they change based on the view template.
So in application.html.erb I have this:
<div class='submenu'>
<%= #app_menu %>
</div> <!-- end submenu -->
And in the view template I have:
#app_menu =
The question is, how do I put the top line (with the link_to, etc) into that instance variable and have Rails parse it properly with the HTML and without errors. Right now if I include <ul><li> it prints them out literally. And, of course, if I include <%= link_to... I get all sorts of errors.
Or is there perhaps a better way?

What I ended up doing was creating partials for each menu, so one partial _actors_menu.html.erb might look like this:
<%= image_tag "submenu-separation.png", :alt => 'separator' %> <li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
<li><%= link_to "Actors with Ampersand (&)", actor_path('search')+'/acute' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
Then I put #app_menu = "actors_menu" on each actor view. And so on for the other pages.
But I'm wondering if there is a smarter way to do all this?
Also, where and how would I set a global variable for the app_menu so as not to repeat it in every template?

Related

ckeditor doesnt show anything in the edit form and the dbms doesnt even load in the editor

I tried the ckeditor and it works fine when adding or making a new post but the problem is when I try and edit the post.
it doesn't load properly and it doesnt show anything like the code , or even a spec, it just shows the pre default ckeditor list and it doesn't even show any data coming from the database.
any help would really be great, and info would be awesome.
I have made it into a form and then rendering it so that I can just call the function like so.
This is the _form
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text, :value => 'Default value', :id => 'sometext' %>
</p>
Now this is in my edit
<%= render 'form' %>
I tried adding the raw into it like so
<%= raw render 'form' %>
or even into the _form but it doesn't seem to show properly
after tinkering and trying the usual thing the codes show it.
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
when I try it and add this now it shows here , but the ckeditor doesn't show properly.
Just give this:
<p>
<%= f.label :text %><br>
<%= f.cktext_area :text %>
</p>
I hope you are calling this partial inside a form_for.
Update the answer,
Just remove
:value => 'Default value',

How to Accept ONLY Two Keys Before User :Submit?

I want people to be able to challenge their habits. If they miss a day they may put "/" if they miss two days in a row they must put "X". Three X's means they failed the challenge.
How can I force the User to only be able to submit X's and /'s as valid keys in the :missed input field?
_form excerpt
<%= simple_form_for(#habit) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :missed %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Github: https://github.com/RallyWithGalli/ruletoday
Thanks in advance for your help. You rock!
You can use a select tag to limit users' options like this
<%= select_tag(:missed , options_for_select([['/', '/'], ['X', 'X']])) %>

Syntax error using form field in Sinatra

I keep getting the following syntax error in my index.html.erb file. I'm using Ruby 2.1.2. and Sinatra 1.4.5. Any comments are appreciated.
syntax error, unexpected ')' #_out_buf = ''; #_out_buf.concat(( form_for(#user) do |f| ).to_s)
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions"><%= f.submit "Sign Up"%></div>
<% end %>
When opening a block in an ERB template like you do with your form_for call, you can't use the <%= tag here. While this is required in Rails, it is only because they patch ERB in a rather horribly way to implement this.
If you do not use Rails, you have to use this:
<% form_for(#user) do |f| %>
<%# ... %>
<% end %>
Finally, unless you use an external library to implement this, note that form_for is not available in standard Sinatra.

How can I add an EM tag to link_to?

I need this output:
<li><em>Name</em><img src="images/9.jpg" alt="image" /></li>
and I dont know how to add EM tag to link_to.
my code is:
<li><%= link_to image_tag(pic.picture.url(:thumb)), pic, :title => pic.name %></li>
The answer provided by #mbratch is definitely the cleaner way to go, however to answer your specific question you do it like this using the block form of link_to:
<li>
<%= link_to pic, title: pic.name do %>
<em><%= pic.name %></em><%= image_tag(pic.picture.url(:thumb)) %>
<% end %>
</li>
You could add a class to the link_to, using :class => 'bold_link' for example, and then in CSS define a.bold_link to have font-weight: bold.

add form (non-nested) dynamically in rails

pls can someone explain how I can dynamically add another copy of a form in rails?...been working on this for almost 2hrs. I messed around with .clone() and .appendTo in jquery, but it didnt work. Also, most of the materials I found online (like on railscast #196 and stackoverflow) focused heavily on nested forms. My form is nested, but I actually just want to add the parent form again. The photos which are nested use the html multiple attributes so I'm guessing that will handle multiple files upload for each parent form (btw I'm using paperclip).
If I just need to modify the railscast code please let me know.
Thanks.
<%= form_for(#user_book, html: { multipart: true }) do |f| %>
<% if #user_book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user_book.errors.count, "error") %> prohibited this user_book from been saved:</h2>
<ul>
<% #user_book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<p>hello</p>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :title, placeholder: "enter title...", id: "book_title" %>
<%= f.text_field :category, placeholder: "enter category..." %>
<%= file_field_tag 'user_book[user_book_photos_attributes][][photo]', :multiple => true do |p| %>
<%= p.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Resources