Having problem with routes.rb. No route matches show action - ruby-on-rails

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" %>

Related

Rails 4: Routing with partials

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 %>

Can't use link path

Hello i'm new one in ruby on rails. I faced with strange behavior. I have in my routes
get 'diary/add_record', to: 'diary#add'
I add link
<%= link_to 'Добавить запись', diary_add_record_path, :remote => true, :'data-type' => 'html', :id => 'new-record-link' %>
and get this
undefined local variable or method `diary_add_record_path'
But when i use different route (main diary) it works fine. Can anyone tell me what wrong with it.
There are two options
i) add a custom route name and use it
get 'diary/add_record', to: 'diary#add', :as => add_diary
your link becomes
<%= link_to 'Добавить запись', add_diary_path, :remote => true, :'data-type' => 'html', :id => 'new-record-link' %>
ii) Do rake routes and find out the route rails generated for your path
rake routes | grep 'add_record'
and use that in your link

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.

Auto_complete_for question

I've recently installed this plugin, and I meant to create a Tag field with it, like StackOverFlow does.
When I put the following syntax on my AnnouncementsController(I want to tag announcements) it works great:
auto_complete_for :announcement, :title
protect_from_forgery :only => [:create, :delete, :update]
Also, I had to add the routes syntax as well to make it work:
map.resources :announcements, :collection => {:auto_complete_for_announcement_title => :get }
Now, when I try to accomplish the same with the tags, at the time I create a new announcement, I simply replace the word "announcement" for "tag" and "title" for "name", and it won't work. Tag makes reference for my Tags table at the database.
The error says the following:
<h1> ActiveRecord::RecordNotFound
in AnnouncementsController#show </h1>
<pre>Couldn't find Announcement with ID=auto_complete_for_tag_name</pre>
Can anybody tell me what I'm doing wrong?
Thanks,
Brian
In your view you probably want to change:
<%= text_field_with_auto_complete :announcement, :title %>
to:
<%= text_field_with_auto_complete :tag, :name %>
to make it work, take another look at the error it's giving, it's still calling announcement.
--- edit:
from autocomplete source:
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
Well, I finally got the answer to my problem.
I was missing the following at routes.rb:
map.auto_complete '/:controller/:action',
:requirements => { :action => /auto_complete_for_\S+/ },
:conditions => { :method => :get }
My new question now it works is the following:
What if I wanted to multi-tag an announcements, for example: "Ruby, C#". Should I change the plugin's logic or is there a functionality to make this work? Cause right now, it will check for the text_field text, not discriminating a new word after a comma or any kind of separator.
Thanks,
Brian

Resources