Rails no route matches controller - ruby-on-rails

I am trying to pass both a user id, and a test id to a controller using link_to. Below is my code:
<%= link_to(test.name, user_test_result_path(:userd_id => 1, protocol.id)) %>
and below are my routes:
but I keep getting the following error:
Why is it saying that no route matches :action => show and :controller=>"test_results when according to my routes it does exist?

Dude. It says userd_id here:
<%= link_to(test.name, user_test_result_path(:userd_id => 1, protocol.id)) %>
Spelling matters!
Also, where is that:
{9=>2...}
coming from in your params? I'm guessing you'll have more luck if you do something like:
<%= link_to(test.name, user_test_result_path(id: protocol.id, user_id: 1)) %>

You shouldn't be passing a hash to your path helper. If your path has two segments, :user_id and :id, you would simply invoke helper_name(user_id, id), not helper_name(user_id: user_id, id).
In your case you should be calling
user_test_result_path(1, protocol.id)

Related

How can I pass in parameters into a rails edit link?

Suppose I have a model
class A < ApplicationRecord
...
end
and in a form, I want to link to a specific instance using
edit_a_path
but I also want to submit a parameter, similar to
<%= link_to "New A",
new_a_path(:b=> {:id => #b.id})%>
How can I do this? I tried
edit_a_path(:b=> {:id => #b.id}, #a)
but I get an error. Also, bonus points if you reference the API documentation. I could not find an API reference for these methods.
In my original code, I had a typo, but the correct way to do this is
edit_a_path(#a, :b=> {:id => #b.id})
This seems to work fine for me:
<%= link_to 'Edit', edit_object_path(object, foo: {bar: true}) %>
Started GET "/objects/1/edit?foo%5Bbar%5D=true" for 127.0.0.1 at 2018-02-03 07:45:59 +0000
Processing by ObjectsController#edit as HTML
Parameters: {"foo"=>{"bar"=>"true"}, "id"=>"1"}

RoR: route with optional parameter uses query string

I have the route below, which is not nested, or namespaced - it's a root route.
get 'discover(/:genre)' => 'home#discover', as: :discover, :genre => /[a-zA-Z0-9-]+/i
Which works fine. But calling the path with the below gives an incorrect URL:
<%= link_to g, discover_path(:genre => g.slug) %>
Gives
/discover?genre=house
Which works fine, but I would like it as /discover/house.
Tried many combinations of :genre => g but none change. What am I missing?
Update:
Server restart and this works. Route caching huh?
Try with it
<%= link_to g, discover_path(g.slug) %>
Thanks

No route matches show action error thrown after submitting form with select_tag

I have a select_tag in a form within my Rails 3 app. When I select a vacation, and the form is submitted, I'd like to be routed to the show action on my vacations_controller. Here is the code for my form:
<%= form_tag url_for(:controller => "vacations", :action => "show"), :method => 'get', :id => "song_selector" do %>
<%= select_tag "vacation_id", options_for_select([["Choose your vacation", ""]]+ Vacation.active.collect {|vacation| [ vacation.title, vacation.id ] } ) %>
<% end %>
However, when I try that, I get an error:
No route matches {:controller=>"vacations", :action=>"show"}
I definitely have a route for this:
resources :vacations, :only => [:index, :show]
And the output of rake routes:
vacation GET /vacations/:id(.:format) vacations#show
I know from previous answers that I'm just not passing the ID in the URL as expected. If I raise params it looks like my ID is being passed as a string like so: `"vacations" => "2".
So I'm wondering: How I can construct my select_tag so this is fixed?
You're missing the id in that action.
Try:
<%= select_tag "id", options_for_select([["Choose your vacation", ""]]+ Vacation.active.collect {|vacation| [ vacation.title, vacation.id ] } ) %>
But this will not be ideal either, as the url will likely be something like "/vacations/?id=X".
An alternative is to use javascript and build the url based on the select option, that way you can construct the url the way you like it.

how to add a string to the path in link_to of ruby on rails

For example I have this code:
<%= link_to "Start", start_path(:id=>1,:box=>1)%>
the id and the box are parameters right?
and for example, it generated this url: http://localhost:3000/start?id=1&box=1
How can I add a string to it, to make look like this:
http://localhost:3000/start?id=1&box=1#box_1
Use the :anchor key:
<%= link_to "Start", start_path(:id => 1, :box => 1, :anchor => 'box_1')
And to answer your first question, yes, id and box are parameters. They are passed in to the request as part of the params hash.

Generating unique HTML ids in Rails when using a repeated partial that has form_for

I have a view on my current project which does something like the following(in haml):
-#horses.each do |horse|
= render :partial => 'main/votingbox', :locals => {:horse => horse}
The in the _votingbox.html.haml file I have the following:
%div.votingbox
%span.name= horse.name
%div.genders
- if horse.male
%img{:src => 'images/male.png', :alt => 'Male'}
- if horse.female
%img{:src => 'images/female.png', :alt => 'Female'}
%div.voting_form
= form_for(Vote.new, {:url => horse_vote_path(horse)}) do |f|
= f.label :comment, "Your opinion"
= f.text_field :comment
...
a bunch of labels and input elements follow generated using the form helpers
This will generate working code but it does generate forms with the same ids for all the form elements which makes the HTML invalid once the votingbox partial is rendered a second time.
My first guess at fixing this was to specify a unique :id to form_for but that only applies to the form tag generated by form_for and not any of the tags inside the form_for block.
One definite solution to this problem is to go through and manually define my own unique ids on form_for and all the form elements I use. This is more work than I had hoped for.
Is there an easier or cleaner way to get unique ids in a similar format to the way Rails currently generates them on all my form elements?
I have removed the original answer as it is totally irrelevant to the updated version of the question.
Update: So now we know that you have an unsaved ActiveRecord object passed to the form_for call, the answer becomes simple: You can override any parameter that form_for generates. That includes the element id. And fields_for sets the context for a specific record.
= form_for(Vote.new, :url => horse_vote_path(horse), :id => dom_id(horse, 'vote')) do |f|
= f.fields_for horse, :index => horse do |fh|
= fh.text_field :whatever
…
You can override the autogenerated ids and names of all form_for content with :as like the following:
= form_for(Vote.new, :as => horse.name, {:url => horse_vote_path(horse)}) do |f|
= f.label :comment, "Your opinion"
= f.text_field :comment
So if a given horse.name is foobar, it will generate a comment field whose id is foobar_comment and name is foobar[comment]
But remember to make sure that the dynamic parameter is acceptable as an html id, a horse.name like hor$e is not acceptable as an html id and therefore might break something.
P.S: Sorry for answering very late, but at the time the question was asked, I haven't had learnt anything at all about rails! hope that might help someone out there!

Resources