NameError in index.html.erb - ruby-on-rails

I am having an issue getting my layout to render at localhost:3000/skills. Here is my view code:
This is the index.html.erb view:
<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
this is my error:
NameError in Skills#index
Showing /app/views/skills/index.html.erb where line #16 raised:
undefined local variable or method `c' for #<#<Class:0x000000055b1620>:0x0000000413d338>
and the specific line it is pointing to is:
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
I think the c.id is an invalid param for my database but I am not sure. I will keep trying to fix it and thanks anyone who knows anything. Cheers and good luck!

<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %> <- **c ends here**
</ul>
<% end %>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
You have to move <% end %> after you end using it. I suppose it is after the last
<% if #skills.blank? %>
<p>There are not any skills currently saved in the system.</p>
<% else %>
<p>These are the current skills saved in our system</p>
<ul id="skills">
<% #skills.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<li>
<%= link_to c.title, {:action => 'show', :id => c.id} -%>
<b> <%= link_to 'Edit', {:action => 'edit', :id => c.id} %></b>
<b> <%= link_to "Delete", {:action => 'delete', :id => c.id},
:confirm => "Are you sure you want to delete this skill??" %></b>
</li>
<% end %>
</ul>
<p><%= link_to "Add new Skill", {:action => 'new' }%></p>
<% end %>

Related

Rails form submit operate like a link_to

So ultimately what I'm trying to do is get the form to be split across two different columns with the status to be in one column and the save button in another column next to the link_to OR have the form's submit operate like a link_to. The form automatically applies some CSS that's causing the issue of splitting the form.
<tbody>
<% #training_resource.spud_users.each do |training| %>
<tr>
<td><%= training.full_name %></td>
<% utr = training.user_training_resources.where(training_resource: #training_resource).first %>
<td class="utr-form">
<%= tb_form_for [:admin, utr], url: admin_update_utr_path(utr), :remote => true, :html => {:id=>'form_id'}, :data => {:errors => :inline, :success => admin_training_resources_path} do |f| %>
<%= f.select :status, UserTrainingResource.statuses.map {|k,v| [k.humanize, k]}, selected: utr.status %>
</td>
<td class="table-actions">
<%= f.tb_save_buttons('', admin_update_utr_path(utr)) %>
<% end %>
<%= link_to 'submit', admin_update_utr_path(utr), :onclick => "$('#form_id').submit()" %>
<%= link_to 'Delete', admin_destroy_utr_path(utr), :method => :delete, :data => {:confirm => 'Are you sure you want to delete this?'}, :class => 'btn btn-danger btn-sm' %>
</td>
</tr>
<% end %>
</tbody>
So what I'm trying to figure out is if there is a way to change the form save button to be a link_to. Right now I have it here under link_to 'submit'. It however does not operate like the tb_save_button as it doesn't redirect to the correct location or save.
You could handle this in the controller.
(example)
def create
if utr.save
redirect_to admin_update_utr_path(utr)
else
# render new form or display the previous view
end
end

How to add a method to link_to do ViewHelper in Rails

Thats the original Devise logout link which works:
link_to("Logout", destroy_user_session_path, :method => :delete)
Because of the and the I had to switch to this kind of link. The problem now is the link try to find the user sign_out
Tried:
<%= link_to (destroy_user_session_path, :method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, :method = 'delete') do %>
<i class="icon-off"></i> Logout</span>
<% end %>
Any ideas or suggestions?
I would do,
<span>
<i class="icon-off">
</i>
<%= link_to("Logout",destroy_user_session_path, :method => :delete)%>
</span>

How to add class in rails 2.3.5 to link_to

I have got following link_to tag But I don't know how to add html class attribute to it and explain me code edit_course_path(course) .. does. I am new to rails
<% #courses.each do |course| %>
<li class="list<%=cycle('odd', 'even')%>">
<div class="category-edit"><%= link_to "#{t('edit_text')}", edit_course_path(course) if permitted_to? :edit, :courses %></div>
<div class="category-delete"> <%= link_to(t('delete_text'), course_path(course), :method => 'delete', :confirm => "#{t('delete_confirm_msg')}") if permitted_to? :destroy, :courses %></div>
</li>
<% end %>
Generally like so:
<% for employee in #employees %>
<%= link_to 'Edit', edit_employee_path(employee) if permitted_to? :update, employee , :class => 'some-class'%>
<% end %>

The Mysterious Order-Dependent Checkbox State Array Controller Transmission Anomaly

Here's all of the relevant view code:
<%= form_for(:object) do |form| %>
<p id="batch_ops">
<b>Objects</b>
<%= submit_tag "Do This" %>
<%= submit_tag "Do That",
:confirm => "Really do that?" %>
<p id="capture" float="left">
<%= button_to_function "Operation_1", \
"Element.remove('batch_input_area'); Element.show('get_op1_params')" %>
<%= text_field_tag "op1_save_path", "_op1_full_dir_path_" %>
<%= button_to_function "Operation_2", \
"Element.remove('batch_input_area'); Element.show('get_op2_params')" %>
<%= text_field_tag "op2_save_path", "_op2_full_dir_path_" %>
</p>
<div id="batch_input_area" style="display:none;">
</div>
</p>
<ul id="object_list">
<% #objects.each do |c| %>
<li id="current_object" style="horizontal-align: left" width="auto"><b>
<%= hidden_field_tag('seen_objects[]', c.id) %>
<%= check_box_tag 'chkd_objects[]', c.id, c.object_checked? %>
<%= c.object_address %>
<% if c.data1? %>
<%= c.data1 %>
<% else %>
<%= "_____________" %>
<% end %>
<% end %>
</ul>
<div id="get_op1_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op1_form'}
) do %>
Options: <%= text_field :op1params, :op1_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op1 Run', :confirm => 'Run Operation_1 test on checked objects?' %>
<%= submit_tag 'Op1 Status' %>
<%= submit_tag 'Op1 Stop', :confirm => 'Stop Operation_1 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
<div id="get_op2_params" style="display:none;">
<%= form_tag(
:action => 'index',
:remote => true,
:html => {:id => 'op2_form'}
) do %>
Options: <%= text_field :op2params, :op2_params, :value => 'freq=30 run=1 stop=0' %>
<%= submit_tag 'Op2 Run', :confirm => 'Run Operation_2 test on checked objects?' %>
<%= submit_tag 'Op2 Status' %>
<%= submit_tag 'Op2 Stop', :confirm => 'Stop Operation_2 test on checked objects?' %>
<%= link_to "Cancel", {:action => "index"} %>
<% end %>
</div>
Now here's the twist: Over on the controller, the Operation_1 handler gets the checkbox presence array seen_objects[] and the activated elements of the checkbox state array chkd_objects[] while the Operation_2 handler does not. If I reverse the order of the get_op1_params and get_op2_params form_tag divs shown last above then the Operation_2 handler gets these checkbox arrays but the Operation_1 handler does not.
My question is simply: Why?
I've long since lost count of the different things I've tried to get around this, which included additional hidden_field_tags within the form_tags. Ultimately I gave up and used a far less elegant submit_tag with a text_field_tag in place of the Operation_2 button_to_function and its associated form_tag but still do not understand why I could not make both of these operations work using button_to_function as illustrated above.
If someone can get this to work, please post your solution here. Any suggestion short of a working solution I've probably already seen and tried.

Ruby on Rails - Trouble looping through partial using collection

I am having issues using the :collection command for a partial within a form I am creating in rails. I would ideally like to use the :collection command, so I can easily manipulate this section in my .rjs templates (the form will submit and reload the form when the check box is changed, it's a to-do list).
This code works:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<% for #list in #lists %>
<%= render :partial => #list, :locals => {:f =>f, :complete => FALSE } %>
<% end %>
</ul>
<% end %>
with the partial:
<% if #list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h #list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
This code does not work, but I would like it to use this form:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<%= render :partial => 'list', :collection => #lists, :locals => {:f =>f, :complete => FALSE } %>
</ul>
<% end %>
with the non-working partial:
<% if list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
I get the error:
object[] naming but object param and #object var don't exist or don't respond to to_param: nil. It is referring to this line: <li><%= f.check_box :completed %>. I'm not sure if why this doesn't work and have tried many, many different variations, but I can't get it working. Is the form preventing me from doing this? The form_for code is straight from the Rails Way book for listing multiple objects from one model in a form.
Any help on this would be greatly appreciated.
I think that the problem is you've not got #list defined anywhere when you're using the render :partial with a :collection.
The system is looking for #list to match the list[] when you call f.check_box
you could set #list = list in your partial to get around that. I suppose.
Tim's answer is correct, but I'd probably avoid extracting the partial within the form_for loop altogether. I suppose it's a matter of style, but I think the confusion here isn't really worth the cleanup that the partial represents in this case. I'd probably write a partial that included the whole form.

Resources