How can I send the multiple ids to controller in this view? - ruby-on-rails

I want to send multiple ids that selected with checkboxes to the controller in this code :
<% form_for :product do %>
<table border="1px">
<tr>
<th>
Select
</th>
<th>
Image
</th>
<th>
Product Name
</th>
<th>
Product Description
</th>
<th>
Product Price
</th>
<th>
Categories
</th>
<th colspan="3">
Actions
</th>
</tr>
<% #products.each do |p| %>
<tr>
<td>
<%= check_box_tag "product_ids[]", p.id, false, :id => "product_#{p.id}" %>
</td>
<td>
<%= image_tag p.photo.url(:thumb) , :alt => "#{p.name}" %>
</td>
<td>
<%= link_to "#{p.name}" , edit_product_path(p) %>
</td>
<td>
<%=h truncate(p.description.gsub(/<.*?>/,''),:length => 80) %>
</td>
<td>
<%=h p.price %>
</td>
<td>
<% for category in p.categories.find(:all) %>
<%= link_to "#{category.name}" , category_path(category.id) %>
<% end %>
</td>
<td>
<%= link_to 'Show' , product_path(p) %>
</td>
<td>
<%= link_to 'Edit', edit_product_path(p) %>
</td>
<td>
<%= link_to 'Remove', product_path(p), :confirm => "Are you really want to delete #{p.name} ?", :method => 'delete' %>
</td>
<% end %>
</tr>
</table>
<div id="products_nav">
<%= link_to "Add a new Product" , new_product_path %>
<%= link_to "Add a new Category" , new_category_path %>
<%= link_to "Category page" , categories_path %>
<%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>
</div>
<% end %>
The code is in this line :
<%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>

At first glance it looks OK to me, although the "delete_selected_products_path" route is not typical. Do you have a route specifically set up for this?
In any case, you should be able to send this delete request to the usual method and have the controller check the params.
This Railscast may help.

Related

Rails 5 ActiveRecord Update Serialized Array in Form

I have a field that is a serialized array. It's loaded into my model and accessed in a form:
class Site < ApplicationRecord
serialize :steps, Array
end
<table class="listing" summary="Site list">
<tr class="header">
<th>Name</th>
<th>Step 1</th>
<th>Step 2</th>
<th>Step 3</th>
<th>Actions</th>
</tr>
<% #sites.each do |site| %>
<tr>
<td><%= site.name %></td>
<% site.steps.each do |step| %>
<td><%= step %></td>
<% end %>
<td class="actions">
<%= link_to("Show", site_path(site), :class => 'action show') %>
<%= link_to("Edit", edit_site_path(site), :class => 'action edit') %>
<%= link_to("Delete", delete_site_path(site), :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
I'm trying to update my edit form so that I can edit each "step" in the array.
<%= form_for(#site) do |f| %>
<table summary="Site form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<% a=1 %>
<% #site.steps.each do |step| %>
<tr>
<th>Step <%= a %>
<td><%= text_field :site, :steps, :value => step %></td>
<% a += 1 %>
</tr>
<% end %>
</table>
<div class="form-buttons">
<%= f.submit("Update Site") %>
</div>
<% end %>
The edit form displays the steps field correctly as each individual string in the array. However, when I attempt to submit the form I get the following error:
Attribute was supposed to be a Array, but was a String.
So steps is being submitted as the last entry in the array. How can I display the form correctly and also present the updated array back to the controller for processing?
Your text_field would need to multiple set to true. I believe in your case, something like this should work.
<%= f.text_field(:steps, { multiple: true, value: #site.steps[step] }) %>

Rails REST doesn't show/load the right page - Ruby on Rails

Basically I created a controller, model and view for subjects. Basically I have 6 actions inside my controller and set up a REST inside my routes file to route the right file.
When I entered, http://localhost:3000/subjects/index it shows me the view for show.html.erb instead of the index.html.erb
Here's what my subject controller looks like:
class SubjectsController < ApplicationController
def index
#subjects = Subject.sorted
end
And here's the content of my index.html.erb file.
<% #page_title = "All Subjects" %>
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", new_subject_path, :class => "action_new") %>
<table class="listing" summary="Subject list" border="1">
<tr class="header">
<th>#</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %> </td>
<td><%= subject.name %> </td>
<td class="center"><%= status_tag(subject.visible) %></td>
<td class="center"><%= subject.pages.size %> </td>
<td class="actions">
<%= link_to("View Pages", pages_path(:subject_id => subject.id), :class => 'action show') %>
<%= link_to("Show", subject_path(subject), :class => 'action show') %>
<%= link_to("Edit", edit_subject_path(subject), :class => 'action edit') %>
<%= link_to("Delete", delete_subject_path(subject), :class => 'action delete') %>
<td>
</tr>
<% end %>
<table>
</div>
Also here's what I set up on my routes:
resources :subjects do
member do
get :delete
end
end
Any idea what am I missing?
The answer is simple: in order you access the index page, you need to hit the following URL:
http://localhost:3000/subjects
Obviously, it will be with GET
The reason why you got the error is: since, anything of the format subjects/:id will take you to the show action within SubjectsController, so Rails interprets subjects/index as you are trying to access the page subjects/:id with index as id of the subject. Here's nothing wrong with Rails, since in RESTful web services, you access the index page by ONLY using the plural name of the resource, like in your case http://localhost:3000/subjects

Extra parameter in link_to method

The following is a piece of code for index action. Can somebody explain me the use of a third parameter, :class => 'action show' in the link_to helper in line numbers 27,28 and 29. The code seems to work fine without this as well. I'm a rails rookie and thanks in advance.
<div class="subjects index">
<h2>Subjects</h2>
<%= link_to("Add New Subject", '#', :class => 'action new') %>
<table class="listing" summary="Subject list">
<tr class="header">
<th> </th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center">
<%if(subject.pages!=nil)%>
<%= subject.pages.size %>
<% else %>
<%= "1" %>
<% end %>
</td>
<td class="actions">
<%= link_to("Show", {:action => "show",:id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", '#', :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
It adds the class attribute to the html-tag.
So
<%= link_to("Edit", '#', :class => 'action edit') %>
will be rendered as:
Edit
You can put any html options as third argument to the link_to helper.
http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to

rails loop take 5 and show next 5

Hello i have a loop in my rails project.
i want to display 5 events in the dashboard controller.
is there a way to show the next 5 events?
<% #events.take(5).each do |event| %>
<tr>
<td> <%= link_to event.title, event %> </td>
<td> <%= event.eventdate %> </td>
<td> <%= event.user_id %> </td>
<td> <%= event.created_at %> </td>
<td> <%= link_to "Aanpassen", edit_event_path(event) %> </td>
<td> <%= button_to "X", event , :method => :delete %> </td>
</tr>
<% end %>
so i want to create a link to te next 5 events.
Instead of using loop use will_pageinate gem
see gem will-paginate.
and railscast for will-paginate

RoR - Closing form_tag

How do I close a form_tag? Here's my code:
<%= form_tag :action => 'authenticate' %>
<h1>Already a member?</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Login" class="form_button" /></td>
</tr>
</table>
<hr />
<%= form_tag :action => 'register' %>
<h1>Register</h1>
<table>
<tr>
<td>Username*: </td>
<td><%= text_field("userform", "user_name", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Password*: </td>
<td><%= password_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td>Email*: </td>
<td><%= text_field("userform", "password", :size => "20", :class => "field") %></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Register" class="form_button" /></td>
</tr>
</table>
I tried <% end %> and <% end_form_tag %>, but I got errors. (Unexpected kEND). I've Googled around a bit, and nothing I've seen really helps. Oh, if I delete everything after the horizontal ruler, the form works fine. But I'd like to have two forms on the page...
I'm using Rails 2.3.5.
form_tag also takes a block, inside which you can put the form elements, whereupon it will be closed automatically. From the docs:
<% form_tag '/posts' do -%>
<div><%= submit_tag 'Save' %></div>
<% end -%>
# => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
Short version (see also: http://dev.rubyonrails.org/ticket/7391):
</form>
?
Correct version:
<% form_tag '/someform' do -%>
<div><%= submit_tag 'Submit' %></div>
<% end -%>
My way to fix it is to include the submit button right before the end and make the button not display.
<%= submit_tag('',style: 'width:0;height:0;display:none;') %>
<% end %>
This puts the ending tag right where I need it.
My fix is placing the form tag just above the table tag.
<%= form_tag(update_password_path, :method=> "post") do |f| %>
<p id="notice"><%= notice %></p>
<table align="center">

Resources