Rails 4: Routing with partials - ruby-on-rails

I get the following error when navigating in my Rails application:
No route matches
{:action=>"startnew", :controller=>"tickets", :ticket_id=>4} missing required keys: [:id]
The problem is, I have a route for:
post 'tickets/:id/startnew' => 'tickets#startnew', as: :start_new
This works fine, when I navigate to the URL http://localhost:3000/tickets
But the error occurs when I load the URL http://localhost:3000/tickets?id=4, which in my case needs to work, because I render tickets#list and tickets#show in two partials on tickets#index, where the error above occurs.
When I click on a link on a different page the tickets#index is loaded and the tickets#show is only rendered when I have the parameters in the url (http://localhost:3000/tickets?id=4).
In show I have this part, which causes the problem:
<%= link_to 'Book time', start_new_path(ticket_id: #ticket.id), method: :post, class: "button info block-shadow-info text-shadow", remote: true %>
Can you please give me a hint!

No route matches {:action=>"startnew", :controller=>"tickets",
:ticket_id=>4} missing required keys: [:id]
You have
post 'tickets/:id/startnew' => 'tickets#startnew', as: :start_new
It means it expects :id as a key, but you are sending :ticket_id. Changing your link to below should work.
<%= link_to 'Book time', start_new_path(id: #ticket.id), method: :post, class: "button info block-shadow-info text-shadow", remote: true %>

Related

Having problem with routes.rb. No route matches show action

I am trying to make a button. When you click it, a modal should appear with a form.
I created the button:
<li>
<%= link_to content_tag(:i, nil, class: "fa fa-plus") + " New Rule",new_rule_correlation_engine_rule_path, class: "pull-right panel-button", 'data-toggle' => "modal", 'data-target' => "#new_rule_correlation_engine_rule_modal", "data-backdrop" => "static" %>
</li>
And in the routes.rb, I defined the first route (the resources part was already there):
get 'correlation_engine_rules/new_rule' => 'correlation_engine_rules#new_rule', as: 'new_rule_correlation_engine_rule'
resources :correlation_engine_rules do
post :apply, on: :collection
end
I also created a file named new_rule, with a logger at the beginning to know if this is being loaded. Apparently, when I click the button, it load that file, because I see the logger, but then I get this error:
<ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"correlation_engine_rules"} missing required keys: [:id]>
So, the routes matches with my first line, but it seems it is also trying to execute the method show, I guess because of the resources lines below mine. At first I had my route below that part, then I saw this error and I put it before, as stated in the routing rails guide, but I still get this error. I thought that when 1 route matches, it stop looking for more, maybe I am wrong but I don't get what is the problem. Thanks.
solution: i finally solved this by entering the new route inside the resources block, like this:
resources :correlation_engine_rules do
post :apply, on: :collection
get :new_rule, on: :collection
end
and then i'm calling it like new_rule_correlation_engine_rules_path.
have you tried specifying the http method GET?,something like this
<%= link_to content_tag(:i, nil, class: "fa fa-plus") + " New Rule",new_rule_correlation_engine_rule_path, method: :get, class: "pull-right panel-button", 'data-toggle' => "modal", 'data-target' => "#new_rule_correlation_engine_rule_modal", "data-backdrop" => "static" %>

Rails no route matches controller

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)

Rails form using GET request: How to remove button and utf8 params?

I'm just trying to create a simple select menu that takes you to a specific URL. So far I have something like this:
# haml
= form_tag new_something_path, method: :get do
= select_tag :type, options_for_select(my_array)
= submit_tag 'New Something'
However, when I submit the form I get the UTF8 parameter as well as a "commit" parameter with the text of the button.
How can I remove the UTF8 and commit parameters?
Removing the commit param is relatively simple, you need to specify that the input does not have a name:
submit_tag 'New Something', name: nil
Regarding the UTF-8 param...it serves an important purpose. Once you understand the purpose of the Rails UTF-8 param, and for some reason you still need to remove it, the solution is easier than you think...just don't use the form_tag helper:
# haml
%form{action: new_something_path, method: 'get'}
= select_tag :type, options_for_select(my_array)
= submit_tag 'New Something', name: nil
You can get rid of the utf8 param by adding the enforce_utf8: false option of form_tag (and also form_form) like the following:
= form_tag new_something_path, method: :get, enforce_utf8: false do
(thanks to #Dmitry for pointing that out)
But please make sure you don't need it: What is the _snowman param in Ruby on Rails 3 forms for? (I'm not sure if it is actually relevant for GET forms.)
The additional parameter is generated by the submit button can be removed by setting the name: false option on your submit_tag (Also works for submit in case of form_for).
= submit_tag 'New Something', name: nil

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.

link_to - make 2 object values become a link

I need to make a link with the following: '#grp.id -- #grp.captain.name'
I tried the code below:
<%= link_to #grp.id--#grp.captain.name, :controller => :groups, :action => :edit_grp, :id => #grp.id %>
But am getting the following error message:
wrong number of arguments (2 for 0)
My question is how do i make the 2 obj values a link?
Thanks for any suggestion
<%= link_to "#{#grp.id}--#{#grp.captain.name}", edit_group_path(#grp) %>
should do what you need in any recent version of rails.

Resources