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
Related
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" %>
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)
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
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.
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