为什么在点击提交时会出现此错误?有没有什么办法解决这一问题?? 这对我来说似乎是正确的......
找不到没有身份证的女孩
我传递参数
的观点<%=form_for :girls, url: url_for( :controller => :girls, :action => :comment ) do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<%= f.hidden_field :id, :value => @girl.id %>
<div class="actions">
<%= f.submit %>
<% end %>
在控制器中评论行动
def comment
@girl = Girl.find(params[:girls][:id] )
@user_who_commented = current_user
@comment = Comment.build_from( @girl, @user_who_commented.id, params[:girls][:body] )
redirect_to :controller => 'girls', :action => 'show', :id => params[:girls][:id]
flash[:notice] = "comment added!"
end
我的路线
Example::Application.routes.draw do
root :to => "top#index"
devise_for :users, :controllers => { :registrations => "registrations" }
get 'girls', :to => 'girls#index', :as => :user_root
match '/girls/comment' => 'girls#comment', :via => :post
get "girls/show"
resources :girls
resources :home
devise_for :users do get 'logout' => 'devise/sessions#destroy' end
resources :girls do
collection do
get 'tag'
end
end
end
答案 0 :(得分:2)
像这样更改您的代码
@comment = Comment.build_from( @girl, @user_who_commented.id, params[:girls][:body] )
@comment.save
redirect_to :controller => 'girls', :action => 'show', :id => params[:girls][:id]