【问题标题】:unable to pass text field value to controller in rails 4无法将文本字段值传递给rails 4中的控制器
【发布时间】:2014-08-28 14:09:44
【问题描述】:

我正在尝试为产品创建 cmets。不知何故,我无法将 text_field 中的值传递回 cmets 控制器。评论在数据库中创建,但表的正文列未填充。

我的产品模型是这样的 -

class Product < ActiveRecord::Base
    has_many :comments
    accepts_nested_attributes_for :comments
end

我的评论模型看起来像这样 -

class Comment < ActiveRecord::Base
belongs_to :product

end

我的评论控制器看起来如下 -

class CommentsController < ApplicationController
def create

@product = Product.find(params[:product_id])
@comment = @product.comments.build(body: params[:comment_body])
@comment.user_id = session[:user_id]
@comment.product_id = params[:product_id]
if @comment.save

            redirect_to selection_path(params[:product_id])
        else
            redirect_to selection_path(params[:product_id]), notice: "Please include a plain text comment only"
        end
      end
      private
      def comment_params
        params.require(:comment).permit(comments_attributes: [ :body,:product_id ])
        end
    end

路线如下-

get "store/prodselect/:id" => 'store#prodselect', as: :selection
resources :products do
get :who_bought, on: :member
post "comments/create" => 'comments#create', as: :create_comment
end

我可以使用以下代码在 prodselect.html.erb 中显示 cmets -

<% @comments.each do |comment| %>
<tr>        
<td class="tbody" style="width:150px;"><%= comment.uname %>
    <%= image_tag @product.user.pic.url(:thumb), :width=>50, :height=>50 %>
</td>
    <td class="tbody" style="width:350px;"><%= comment.body %></td>
</tr>   
<% end %> 

这是我无法将 text_field 值传递回 cmets 控制器的地方。下面的代码和上面的代码都在 prodselect.html.erb 中。 prodselect 也是 Store 控制器中的一种方法 -

<tr><td>                    
<%= text_field :comment, :body%>
<%= button_to 'Add comment' , product_create_comment_path(@product.id), :class => "buttonto" %>
</td></tr>

最后我在商店控制器中的 prodselect 方法看起来像这样 -

def prodselect
    @product = Product.find(params[:id])
    @comments = Comment.where(product_id: params[:id])
    @comment = Comment.new
  end

我是 ror 的新手,所以任何指针都将不胜感激。我想知道为什么我无法将我的文本字段值传递给我的 cmets 控制器。我也尝试过使用 text_area 失败。

提前致谢

【问题讨论】:

  • 您能发布您的完整表格吗?您的代码中有很多东西搞砸了
  • 您可以发布params 来参与您的控制器操作吗?你可以在 Rails 服务器终端上找到它
  • 嗨,这是在日志中传递的参数 - "comment"=>{"body"=>"Great Comment"} 。如何访问控制器中的值?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

您使用的是 form_for 还是类似的东西?只有路径的 button_to 不会将信息发送到您的控制器。尝试阅读this

【讨论】:

    【解决方案2】:

    button_to 自己创建一个表单,只是发布到 url(我承认我解释得不好,所以去看看链接的文档),所以你的文本字段实际上不是表单的一部分,并且因此没有通过。您将需要使用实际表格

    <tr>
      <td> 
        <%= form_for [@product, Comment.new] do |f| %>
          <%= f.text_field :body %>
          <%= f.submit 'Add comment', :class => "buttonto" %>
        <% end %>
      </td>
    </tr>
    

    【讨论】:

    • 您好,当我尝试在部分中使用 form_for 时收到一条错误消息。消息是 'NoMethodError in Store#prodselect' 。显示 E:/demo/app/views/store/_form.html.erb 其中第 1 行出现:未定义的方法 `product_cmets_path' for #:0x4d1dc98>
    • &lt;%= form_for [@product, Comment.new], url: create_comment_product_path do |f| %&gt; 我假设查看您的路线,如果不是 rake 路线,并查看您的评论创建路径是什么,并在 url 选项中使用它。
    【解决方案3】:

    是的,我的路线路径不正确。 form_for 也有帮助。感谢您为我指明正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多