Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Select_tag and view/controller interaction

select_tag controller view model

  • Please log in to reply
4 replies to this topic

#1 moro

moro

    Passenger

  • Members
  • 6 posts

Posted 12 March 2014 - 11:59 AM

Hi guys:

My app has two colums: the first has "static" contents and the second one has the "<% yeld %>".

 

I want in my static columns a simple select form that has to:

- list User.all matching with User.id

- when selecting a row in the selector, pass the id to the Users controller and display on the yeld column the simple :show action.

 

I've read all the pages in the apis, tens of posts in forums, tried a lot of solution but I have no clue on how to do that and my every attempt ends up with some error (route, or undefined methods, I've received every possible error).

 

Can somebody please help me, I'm going mad...

 

thank you



#2 moro

moro

    Passenger

  • Members
  • 6 posts

Posted 12 March 2014 - 07:04 PM

To be more specific I have understood that dealing with models it is wiser and simpler to use the helper collection_select so I put this code

 

# app/view/layout/application.html.erb

[...]

<%= collection_select(:user, :id, User.all, :id, :name) %>
[...]

 

that correctly renders:

<select id="user_id" name="user[id]"><option value="6">Moro</option><option value="7">Irid</option></select>

 

how can I pass this hash to the method :show of Users controller??

 

hope I was clear enough



#3 himanshu339

himanshu339

    Passenger

  • Members
  • 9 posts

Posted 17 March 2014 - 08:18 AM

This needs to be submitted along with form to show action. There you can retrieve this collection as hash.

 

Hope this helps!



#4 moro

moro

    Passenger

  • Members
  • 6 posts

Posted 17 March 2014 - 11:50 AM

Well, I think I understood how it should work, but I'm not able to make it  work; if I'm not wrong:

I should have a collection_select that write on params something like [:user][:id], and "get" that hash to the controller action :show....

 

What I'm not able to do is to submit the form...

 

I understood that dealing with models I should use "collection_select" instead of "select_tag"...but if I'm not wrong I should pass a "get" request and not a "post"...am I right?

Moreover I'm not shure I'm building the form correctly; I tried using a text field instead of a collection_select:

<%= form_for @users, url: {action: "show"} do |f| %>
  <%= f.text_field :id %>
  <%= f.submit "Show" %>
<% end %>

but i get this error on the first line (by the way, in the controller i have @users = User.all:

undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_User:Class

It should be difficult to use a select to show objects instead of the default <% @users.each do |user| %> and <% links_to ...%>


#5 moro

moro

    Passenger

  • Members
  • 6 posts

Posted 17 March 2014 - 02:08 PM

I'm now trying with form_tag and select_tag:

<%= form_tag user_path(:id), method: "get" do %>
  <%= select_tag :id, options_from_collection_for_select(User.find(:all), :id, :name) %>
  <%= submit_tag "Show" %>
<% end %>

the form generated seems to be right:

<form accept-charset="UTF-8" action="/users/id" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" value="✓" type="hidden"></div>
<select id="id" name="id"><option value="6">Francesco Moro</option>
<option value="7">Operatore Irid</option></select>
<input name="commit" value="Show" type="submit">
</form>

but somehow the id is not correctly passed from the select_tag to the controller and whenever i click the button i get this error:

 

Couldn't find User with id=id Request

Parameters:

{"utf8"=>"✓",
 "id"=>"id",
 "commit"=>"Show"}
 

 

Any hint?







Also tagged with one or more of these keywords: select_tag, controller, view, model

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users