【问题标题】:Rails form keeps returning a nomethoderrorRails 表单不断返回 nomethoderror
【发布时间】:2011-08-18 07:06:23
【问题描述】:
        <%= form_for rating_ballot, :html => { :class => 'rating_ballot' } do |f| %>
            <%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"})
            radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>
            <%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"})
            radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>
            <%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"})
            radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>
            <%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"})
            radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>
            <%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"})
            radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>

            <%= hidden_field_tag("message", msg.message) %>
            <%= f.submit :Submit %>
        <%- end -%>

这是我的表单,我正在尝试创建一个系统,让用户在“星”系统表单this 教程中对消息进行排名。我已按照教程中的所有说明进行操作,但是当我包含此表单时,我收到了 nomethoderror undefined method 'ratings_path'。在我的 home_helper 我有

  def rating_ballot
    if @rating = current_user.ratings.find_by_id(params[:msg])
      @rating
    else
      current_user.ratings.new
    end
  end

  def current_user_rating
    if @rating = current_user.ratings.find_by_id(params[:msg])
      @rating.value
    else
      "N/A"
    end
  end

为什么会出现此错误?谢谢

【问题讨论】:

    标签: ruby-on-rails ruby forms nomethoderror


    【解决方案1】:

    这是一个路由问题。 *_path 被称为路径助手。运行rake routes 以查看您有哪些路线。您可能需要为您的操作添加新路线才能使用路径助手。从您的代码中,我猜您需要:

    资源:评分

    但是任何人都猜到你的应用程序的其余部分是什么样子的。

    【讨论】:

    • 感谢您的帮助,但没有奏效,我应该发布哪些其他代码才能获得有关此问题的帮助?谢谢
    • 您的 helper 中似乎正在初始化您的操作?您可能想回到绘图板并查找一些 MVC 资源。否则,我更愿意看到你的 controller 而不是 helper。
    • 我的 home_controller 中没有任何与 rating_ballot 相关的代码
    • rake routes 返回什么?你有rating 模型吗?看起来助手只是返回一个评分。
    • 好的,我通过 resources :ratings 而不是 resources :rating_ballot 解决了这个问题。感谢您的帮助。
    猜你喜欢
    • 2019-05-20
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多