视图
<td><%= link_to "+1", :controller => :profiles, :action => :addpoints, :profile_id => profile.id, :task_id => @task.id%></td>
尝试访问profile.id时:profile_id rails返回错误,id根本没有传递。我该如何解决?通过两个模型传递两个参数的最佳方法是什么?
耙路线
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_dashboard GET /static_pages/dashboard(.:format) static_pages#dashboard
static_pages_pending GET /static_pages/pending(.:format) static_pages#pending
static_pages_taskcompleted GET /static_pages/taskcompleted(.:format) static_pages#taskcompleted
tasks_index GET /tasks/index(.:format) tasks#index
tasks_complete GET /tasks/complete(.:format) tasks#complete
tasks_delete GET /tasks/delete(.:format) tasks#delete
profiles_addpoints GET /profiles/addpoints(.:format) profiles#addpoints
profiles_index GET /profiles/index(.:format) profiles#index
root GET / static_pages#home
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
tasks GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
new_task GET /tasks/new(.:format) tasks#new
edit_task GET /tasks/:id/edit(.:format) tasks#edit
task GET /tasks/:id(.:format) tasks#show
PATCH /tasks/:id(.:format) tasks#update
PUT /tasks/:id(.:format) tasks#update
DELETE /tasks/:id(.:format) tasks#destroy
profiles GET /profiles(.:format) profiles#index
POST /profiles(.:format) profiles#create
new_profile GET /profiles/new(.:format) profiles#new
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
DELETE /profiles/:id(.:format) profiles#destroy
答案 0 :(得分:0)
将profile.id
替换为@profile.id
。验证配置文件实际上是一个实例。可能正在传入nil
值(与正确连接的内容相反)。
答案 1 :(得分:0)
我们可以看到方法内容吗?
如果错误与id有关,则路由正确。 params[:profile_id]
中的内容是什么?我们需要更多信息..
答案 2 :(得分:0)
我认为这是你想用Rails 4做的事情。
<%= link_to "+1", profiles_addpoints_path(:profile_id => profile.id, :task_id => @task.id) %>
上面的代码会在addpoints
控制器中使用参数profiles
调用{:profile_id => profile.id, :task_id => @task.id}
方法。你可以像这样访问参数。
params[:profile_id]
和params[:task_id]
。
您的代码令人担忧的另一件事是,在routes.rb中,profiles_addpoints
路由是通过GET
HTTP动词调用的。这有潜在危险,因为谷歌搜索机器人将访问这些路径,并导致upvotes飙升。尊重HTTP约定并使用PUT
。