Ruby on Rails: Using Collection_select and edit path - ruby-on-rails

I'm having trouble getting this to work. What I want to do is have a dropdown that populates with team numbers, then depending on which one the user selects it takes them to the edit page for that team. I can get the dropdown to populate correctly, but I can not get it to take me to the correct page. When I test it a different way, it does take me to the correct page so I don't believe that it is a routing error.
The code with issues:
<%= form_tag edit_corevalue_path(#corevalue), method: 'get' do %>
<%= collection_select(:corevalue, :corevalue_id, Corevalue.all, :id, :teamNumber, :prompt => "Select Team") %>
<%= submit_tag "Submit!", :name => nil , class: "btn btn-large btn-primary" %>
<% end %>
I get the error:
No route matches {:action=>"edit", :controller=>"corevalues", :id=>nil}
But if I comment that out and test it with:
<%= link_to "TEST", edit_corevalue_path(2) %>
Then it works and takes me to the correct page. I believe the error is that it is not getting the correct id when using #corevalue, but I'm not quite sure how to fix it or what the proper syntax would be. Any help is greatly appreciated. Thank you.

I think you want to use :id instead of :corevalue_id as the 2nd argument to the collection_select function:
<%= collection_select(:corevalue, :id, Corevalue.all, :id, :teamNumber, :prompt => "Select Team") %>
See ActionView reference

Related

How to Pass Form Field Values to a Different Controller View in Rails?

In my Rails 6 app, I have a Product and Order model.
On my products#show page, I have some fields and a button. What I need to do is send the info to the orders#new page so that this data is shown on the orders#new page.
I have tried to write some code based on Pass variables without model associations in Rails and how to pass parameters in params using form_tag method in rails, but my code seems completely wrong.
on products#show:
<%= form_tag(new_order_path do |form| %>
<%= form.input_field :comments %>
<%= form.button %>
<% end %>
With this code I get undefined method text_field' for nil:NilClass`.
I have tried adding attr_accessor :comments to both the Product and Order model, but it doesn't help.
I don't think that my approach or what I am trying to code is right. I was just trying to piece together parts from these answers.
Can someone please help me figure out the best way I can pass this data to Orders#new to show in that view?
<%= form_with url: "/search", method: :get do |form| %>
<%= form.label :query, "Search for:" %>
<%= form.text_field :query %>
<%= form.submit "Search" %>
<% end %>
from the docs
https://guides.rubyonrails.org/form_helpers.html
and the show path also requires an id

Rails: How do I associate a submit_tag to a search_field_tag?

I have the following code in my view:
<% form_tag(search_path) %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
And the following route:
search_path POST /search(.:format) searches#create
The form appears correctly but when I click submit nothing happens. Server logs show absolutely nothing. Chrome Dev tools shows nothing happens on the client side. I think that the way I am constructing this form is incorrect. I think I need to add something to associates the different tags I have, and without this element all these form elements are all disassociated and fragmented. What am I missing here?
You need to add do to the end of your form_tag. This is how Rails associates the different elements of the form.
<%= form_tag(search_path) do %>
<%= search_field_tag("search", nil, placeholder: "Book name") %>
<%= submit_tag ("Search") %>
<% end %>
"Doing-Ending" the form_tag is a good practice, but I'd say your only problem is the lack of the = sign in the embedded Ruby syntax.

Small form to move associations

I've got a group which has many clients.
The moment the user wants to delete the group, he's obligated to move the clients of that group to a different group.
So i've created a little form. But I already know it's wrong. but I don't know how I should do it otherwise.
I want the form to be caught by my disband_group method in my groups controller. Extract the move_to ID etc.
This is what I have now. Which doesn't work.
(it says param not found: group on params.require(:group) which is no big surprise)
<%= form_for(#group) do |f| %>
<%= select_tag('move_to', options_for_select(current_user.groups.collect{ |u| [u.name, u.id] }), :prompt => "Choose") %>
<%= f.submit "Move" %>
<% end %>
The select_tag needs to be changed to f.select. That should do the trick.
After that if it still does not work, put a 1/0 as the first line in the method to check the parameters coming in. This will throw an exception and the page will display the parameters.
Add a route to your disband_group action (add the correct path):
post '/disband_group' => "groups#disband_group", :as => :disband_group
And then modify your form pointing to the disband_group action:
<%= form_for(:group, url: disband_group_path) do |f| %>
<%= f.select :move_to, options_for_select(current_user.groups.collect{ |u| [u.name, u.id] }), :prompt => "Choose") %>
<%= f.submit "Move" %>
<% end %>
This will add send the move_to param as follows: "group" => { "move_to" => "3" }
Take a look to this to understand how the form works.

RoR 3.2.5 Railscasts #102: Autocomplete: Form is prepopulated with data (should be blank)

I'm going off Railscasts 102.
My current issue is that my search form is already populated with data when the page loads.
I have one model, listing, and 5 columns on that model: website, url, comments, doctor, and date. In this case I am just worrying about website.
My listings.js.coffee is
jQuery ->
$('#search').autocomplete
source: $('#search').data('autocomplete-source')
And my index.html.erb file (the important part) is
<%= form_tag '/listings', :method => :get do %>
<%= text_field_tag :search, data: {autocomplete_source: Listing.order(:website).map(&:website)} %>
<%= submit_tag 'Search', :class => 'echo-search-tag', :name => nil %>
<% end %>
When I load the page, the search form is already populated with
{:data=>{:autocomplete_source=>["Doctors4US", "How To Get Better", "Rafael's Epic Doctor Site", "Testing Admin", "WebMD"]}}
Obviously this shouldn't be the case. The JS console is giving me an error of:
Uncaught TypeError: Property 'source' of object # is not a function
I saw another Stack Overflow post in regards to this, I tried that solution but it didn't make a difference.
Note: when I tried test data for the source in listings.js.coffee, like ['foo', 'fore', 'food'], the autocomplete was working correctly.
Also note that the search function is working correctly aside from autocomplete.
Any input is appreciated :)
Ok the issue was that the text_field_tag takes 2 arguments, and that is why the search form was being prepopulated with the data.
So I just added nil for the 2nd argument
<%= text_field_tag :search, nil, data: {autocomplete_source: Listing.order(:website).map(&:website)} %>
And voila! Working.
Now trying to figure out how to add multiple columns to Listing.order and .map (ie, having not only :website, but :comments, :doctor, and :url). Input appreciated!

rails form_tag url path

This seems like it should be so simple but it has been causing me issues.
I have a select_tag that pulls from a model. All I want to is have a person choose their location from the drop down, press submit and take it to that places page.
Here is what I have
<% form_tag installation_path do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<div id="button">
<p>
<%= submit_tag "Go", :name => nil %>
</p>
</div>
The problem is it of course wants an :id but it won't pull the :id from the drop down menu below.
What am I doing wrong, any other suggestions on the "right" way to do this.
Thanks
Looks like you actually want to GET, not to POST the params.
form_tag installation_path, :method => :get do

Resources