ruby-on-rails - 为什么无法正确接收参数?

标签 ruby-on-rails ruby-on-rails-3 view controller parameter-passing

为什么当我点击提交时会出现这个错误?有没有什么办法解决这一问题?? 这些对我来说似乎是正确的......

Couldn't find Girl without an ID

我对传递参数的看法

<%=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 %>

Controller 中的评论操作

  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

最佳答案

像这样改变你的代码

@comment = Comment.build_from( @girl, @user_who_commented.id, params[:girls][:body] )
@comment.save
redirect_to :controller => 'girls', :action => 'show', :id => params[:girls][:id]

关于ruby-on-rails - 为什么无法正确接收参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380684/

相关文章:

android - 扩展 EditText 以赋予它额外的 "default"功能的正确方法是什么

mysql - Rails 如何在实例不存在的情况下查询 has_many 实例

ruby-on-rails - 多对多关系获取的脚手架表

ruby-on-rails - 参数 'args=(not_set = true)'在ruby中是什么意思?

javascript - 在不刷新页面的帖子中添加“赞”按钮

javascript - 带推杆/faye 的 rails 应用

android - 是否存在具有 View 回收功能的 Gallery 小部件的替代品?

Android 忽略我的 setWidth() 和 setHeight()

ruby-on-rails - 使用音译的 Rails 参数化不适用于阿拉伯语/Unicode 字符串

mysql - Rails - 没有这样的表 [rake db :migrate]