ROR link_to method problem
I'm trying to pass some values through a link and I want them to be invisible. These are the options I've tried:
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => @idea.id, :user_id => @idea.user.id, :method => :post %>
<%= link_to 'Add comment',{ :controller => :comments, :action => :new, :idea_id => @idea.id, :user_id => @idea.user.id}, :method => :post %>
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => @idea.id, :user_id => @idea.user.id, %>
<%= link_to 'Add comment', new_comment_path, :idea_id => @idea.id, :user_id => @idea.user.id, :method => :post %>
First option - treats method as a parameter: http://localhost:2000/comments/new?idea_id=1&method=post&user_id=1
Second option - goes lik开发者_开发技巧e this: http://localhost:2000/comments/new?idea_id=1&user_id=1 and also causes routing error: "Routing Error No route matches "/comments/new"
Third option - loads the form, but of course is like: http://localhost:2000/comments/new?idea_id=1&user_id=1
Fourth option - looks good (http://localhost:2000/comments/new) but the same routing error like the second one.
What am I doing wrong?
Thanks in advance.
PS
I was asked to give my routes, so here they are:
resources :rights
resources :comments
resources :ideas
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'main#home'
#match '/comments/new' => "comments#new" # this doesn't help
match '/home', :to => 'main#home'
match '/contact', :to => 'main#contact'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/signup', :to => 'users#new'
If you have RESTful routes
<%= link_to 'Add comment', new_comment_path,
:idea_id => @idea.id, :user_id => @idea.user.id, :method => :post %>
should be
<%= link_to 'Add comment', comments_path,
:idea_id => @idea.id, :user_id => @idea.user.id, :method => :post %>
As others have said, it sounds like you have a problem in your routes file. Either check if you have the resource :comments
defined or post your routes file here and we'll help you out. It's possible that it's not working because you are trying to POST...
If you want 'invisible' variables (I assume you mean that you do not want the variables to appear in the URL), you'll have to POST to the page rather than just link to it. In this case, your second example is the best bet. It goes against convention to POST to /new so that could be what is causing the 'no routes' error if you're using resource :comments
Give this a try in your routes.rb:
match '/comments/new' => "comments#new"
Give that a try, it should load the correct page and give you access to the variables you passed through to it through params
.
Please note, that this goes wildly against convention. Is there a reason why you don't want those variables to appear in the URL? It's likely there's a better way of doing what you're thinking and if you explain we can advise you as best as we can.
Have you properly defined the routes?
Can you show how they are?
You should have something like this for that to work: resource :comments
Also, in general /new works with a GET, and a POST is sent when creating...
The method is part of html_options, you need to separate the two hashes like so:
<%= link_to 'Add comment', {:controller => :comments, :action => :new, :idea_id => @idea.id, :user_id => @idea.user.id}, :method => :post %>
精彩评论