Rails 4, Route with multiple ids - ruby-on-rails

I want to make a route like this /p/:id/:ph_id. and the :ph_id also has limitation on length /\d{7}/
This is what I have for the first id
scope '/p', :controller => 'people' do
scope '/:id', :id => /\d{7}/ do end
end

In your config/routes.rb try following code:
get "/p/:id/:ph_id" => "people#show", as: :my_route, id: /\d{7}/, ph_id: /\d{7}/
You can use it like:
<%= link_to "Check this link!", my_route_path(id: 1234567, ph_id: 7654321) %>
Hope that helps! Good Luck!

Related

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)

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

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.

remote_function keeps adding authenticity token on GET requests

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'});
) %>

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