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
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" %>
Have tried forums, documentation and blog suggestions. They converge on syntax that for me does not properly save AND route through desired controller.
Big picture, I have two software applications that share functionality. To implement shared functionality, I had Rails generate Shared::Widgets. This MVC works just fine. No problem viewing, updating, creating, or deleting using standard shared/widgets & etc routing.
Then, for the product-specific functionality, I created two controllers: Product1::Widgets and Product2::Widgets. Both inherit from Shared::Widgets controller. Both are empty except for product-specific layouts.
This scheme almost works. When I route to product1/widgets, it sets layout to product1 and invokes index method of shared/widgets. The result is display of shared/widgets/index.html.erb with product1 layout. Similarly, when I route to product2/widgets, this sets product2 layout and invokes index method of shared/widgets. The result is display of shared/widgets/index.html.erb with product2 layout. Perfect.
But now we get to the form_for. Because it implements the rails magic, it really really wants to route directly to Shared::Widgets controller. But that's not what I want. I want it to route to the appropriate product controller so as to set layout. The rails generated form_for was something like this:
form_for(#shared_widget, :html => { :class => "form"}) do |f|
I tried:
form_for([:product1, #widget], :html => { :class => "form"}) do |f|
but that duplicates namespace (product1_widget_shared_widget_path).
I tried the following three formats, all of which seem to route correctly, and save the record, but the record is empty (columns are blank):
form_for(#widget, url => "product1/widget", :html => { :class => "form"}) do |f|
form_for(#widget, url => url_for(:controller => "widget"), :html => { :class => "form"}) do |f|
form_for(#widget, url => url_for(:controller => "widget", :action => "create"), :html => { :class => "form"}) do |f|
Any help? If the above code has spelling errors, it is due to transcription. The actual code I used passed the interpreter. Thank you.
try this
form_for(url: { controller: "posts", action: "create" }, html => { :class => "form" }, method: "post") do |f|
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.
The following link_to statement:
<%= link_to image_tag("icons/document_24.png"),
[program_code.program, program_code],
:class => :no_hover,
:alt => "Print Tracking Code",
:title => "Print Tracking Code",
:target => :new
%>
will generate a url like /programs/1/program_codes/1
If I want the url to be /programs/1/program_codes/1.svg, how do I specify the format in the array that is being passed to url_for? I've searched the Rails API documentation and looked at several examples but have been unable to find anything like this.
I think your looking for the :format option. It will append the file extension to the link e.g. '.svg'
Make sure you put the :format option in the path building hash of the link_to method.
<%= link_to 'somewhere', {somewhere_to_path(#var), :format => "svg"},:title => "Print Tracking Code", :target => "_blank" %>
Hope this helps.
If you are dealing with a specific class and can use a named route, that is the most efficient option. But if you're dealing with nested resources and the parent resource isn't fixed (for instance, in a polymorphic association), AND you want to specify a format, url_for doesn't meet your needs.
Fortunately you can use polymorphic_url.
For instance, if server could be an instance of ManagedServer or UnmanagedServer, and both can have alerts, you can do this:
polymorphic_url([server, :alerts], :format => :xml)
and that will give you
/managed_server/123/alerts.xml
/unmanaged_server/123/alerts.xml
I got the problem similar to this post here: https://rails.lighthouseapp.com/projects/8994/tickets/106-authenticity_token-appears-in-urls-after-ajax-get-request
routes.rb
map.namespace(:admin, :active_scaffold => true) do |admin|
admin.resources :regions, :shallow => true do |region|
region.resources :birds, :collection => {:search => :get}
end
end
view
<%= javascript_tag %Q(
#{remote_function(:update => 'bird_search', :url => search_admin_region_birds_path(#region.id), :method => :get)}
) %>
It displays url like:
http://localhost:3000/admin/regions/7/birds/search?authenticity_token=F43BcQUM4z3bl7s21kLZQrqwGkuErF7C9jiNMKFTZTo%3D
which should be:
http://localhost:3000/admin/regions/7/birds/search
Without this working my Ajax pagination won't work... help!
what version of rails are you using?
that ticket says it was closed out, maybe you are on earlier version
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001653
the example output does not have the auth token
Fixed this by using Javascript instead of using RJS.
Many times RJS methods aren't very dependable when your apps get more complicated, so take care there.
Anyway for this problem I changed the code to:
<%= javascript_tag %Q(
new Ajax.Updater('region_birds', '#{of_region_admin_region_birds_path(#region.id)}', {asynchronous:true, evalScripts:true, method:'get'});
) %>