【问题标题】:Displaying form_for for comment method in show view在显示视图中显示 form_for 用于评论方法
【发布时间】:2018-03-11 04:45:54
【问题描述】:

我正在尝试在我的类型模型 (types#show) 视图中显示评论表单

路由定义如下

Rails.application.routes.draw do
  devise_for :users
  root to: "products#index"
  resources :types do
    member do
        get :vote, :as => :vote
      post "vote" => "types#vote"
      get :comment, :as => :comment
      post "comment" => "types#comment"
    end
  end
  resources :models
  resources :products
  resources :brands
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end 

types_controller.rb中的注释方法

  def comment
    user = current_or_guest_user
    @type = Type.find(params[:id])
    user_review = params[:review]
    @type.review(user_review, user)
    redirect_to @type, notice: 'Thank you for your comment.'
  end

我在视图中正确设置 form_for 时遇到问题

<%= form_for comment_type_path(@type) do |f| %>

    <div><h3>How likely are you to recommend our service to your friends, family or colleagues?</h3></div>



    <div><h3>Can you please tell us why you gave us this score?</h3></div>
    <div class="field-box">
      <br>
      <%= f.text_area :review, class: "form-control", rows: "4" %><br>
    </div>




    <div class="actions">
      <%= f.submit 'Vote', class: 'btn-flat success new-product' %>
    </div>

  <% end %>

给我以下错误: 没有路线匹配 [POST] "/types/5"

<%= form_for @type, url: comment_type_path(@type), method: :post do |f| %>
    <div><h3>How likely are you to recommend our service to your friends, family or colleagues?</h3></div>



    <div><h3>Can you please tell us why you gave us this score?</h3></div>
    <div class="field-box">
      <br>
      <%= f.text_area :review, class: "form-control", rows: "4" %><br>
    </div>




    <div class="actions">
      <%= f.submit 'Vote', class: 'btn-flat success new-product' %>
    </div>

  <% end %>

给了

类型中的参数错误#show 显示 /home/jan/Documents/supplement/app/views/types/show.html.erb 其中第 62 行提出:

参数数量错误(0 代表 2)

【问题讨论】:

  • show.html.erb 的第 62 行是什么? ArgumentError 可能是由于您的某些方法调用。你最后的form_for 语法是正确的

  • 你能用你的整体视图和完整的回溯创建gist吗?
  • 我希望这就是你想要的:Gist 谢谢!
  • 我在comment_type_path 中看不到@type here

标签: ruby-on-rails routes form-for


【解决方案1】:

请用以下代码替换您的form_for

&lt;%= form_for @type, url: comment_type_path, method: "post" do |f| %&gt;

【讨论】:

  • 这给出了与上面相同的错误:ArgumentError in Types#show Showing /home/jan/Documents/supplement/app/views/types/show.html.erb where line #62 raise: wrong number此行中的参数(0 代表 2)&lt;%= f.text_area :review, class: "form-control", rows: "4" %&gt;&lt;br&gt;
  • 试试这个&lt;%= f.text_area(:review, class: "form-control", rows: "4") %&gt;
  • 谢谢,还是一样的错误。我很困惑为什么这向我显示 types#show 的参数错误以及缺少哪些参数。这不会从错误消息中显示
  • @asmitakalena 这两个 text_area 调用没有区别
  • 注:上面确实是一样的。请无视。我的错 好的,请提供as 选项以及您的发帖请求路线。类似post "comment" =&gt; "types#comment", as: "comment" 并在 form_for url 中使用生成的辅助路径
猜你喜欢
  • 1970-01-01
  • 2017-01-26
  • 2014-09-09
  • 1970-01-01
  • 2011-10-02
  • 2013-11-09
  • 1970-01-01
  • 2013-06-05
  • 2014-11-04
相关资源
最近更新 更多