[英]rails - undefined method `model_name'
I have a basic blogging site in which I want to allow users to offer 'corrections' to posts (just think of it as a comment). 我有一个基本的博客网站,我希望允许用户对帖子提供“更正”(只需将其视为评论)。 The correction object belongs to a post, which in turn belongs to a user (for which I'm using Devise).
校正对象属于一个帖子,后者又属于一个用户(我正在使用Devise)。
I would like the form to create a new correction to be nested in the page for the post,so I'm just rendering the form in posts/show.html.erb with <% render :template => "corrections/new" %>
. 我希望表单能够创建一个新的修正嵌套在帖子的页面中,所以我只是在posts / show.html.erb中使用
<% render :template => "corrections/new" %>
渲染表单<% render :template => "corrections/new" %>
。 I'm getting a 'undefined method model_name' error from the line form_for line in corrections/new.html.erb though. 我从corrections / new.html.erb中的line form_for行获得了“未定义的方法model_name”错误。
Here's the form: 这是表格:
<% form_for [@correction, :url=> user_post_corrections_path(current_user, @post, @correction)], html: { multipart: true} do |f| %>
<div class="field">
<%= f.label :correction %>
<%= f.text_field :correction %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the corrections_controller: 这是corrections_controller:
class CorrectionsController < ApplicationController
def new
@post = Post.find(params[:post_id])
@correction = current_user.@post.corrections.build
respond_with(@correction)
end
def create
@post = Post.find(params[:post_id])
@correction = current_user.@post.corrections.build
if @correction.save
redirect_to user_post_path(current_user, @correction.post_id)
end
end
end
In my routes.rb: 在我的routes.rb中:
resources :users do
resources :posts do
resources :corrections
end
end
Any help with this would be much appreciated. 任何有关这方面的帮助将非常感激。
There are several issues I can see, first off: 我可以看到几个问题,首先是:
The form is not rendered: The form for should be evaluated not computed so you should use <%= %> tag instead of <%%> tag, so it becomes: 表单未呈现:应评估表单不计算,因此您应使用<%=%>标记而不是<%%>标记,因此它变为:
<%= form_for @correction, url: user_post_corrections_path(current_user, @post, @correction), html: { multipart: true} do |f| %>
Second: You are saying form_for @correction, and you said this form is shown in posts/show, this means that the show action for the post should have the following in its controller: 第二:你说的是form_for @correction,你说这个表单显示在posts / show中,这意味着post的show动作应该在其控制器中有以下内容:
@correction = Correction.new user: current_user, post: @post
This is assuming you are using the @post variable for the post show meaning you have something like 这假设你正在使用@post变量来表示你有类似的东西
@post = Post.find params[:id]
This line is completely off 这条线完全关闭了
@correction = current_user.@post.corrections.build
you can just say: 你可以说:
@correction = Correction.new correction_params
@correction.user = current_user
@correction.post = @post
if @correction.save
redirect_to user_post_path(current_user, @correction.post_id)
end
The important part is your show action on the posts controller having the initialization of your @correction object. 重要的部分是你在post控制器上的show动作,它具有@correction对象的初始化。
This line looks off: 这条线看起来:
@correction = current_user.@post.corrections.build
@post
is an instance variable, not a method on current_user
@post
是一个实例变量,而不是current_user
上的方法
Did you intend to build a correct with the current user? 您打算与当前用户建立正确的吗? The following should work:
以下应该有效:
@post.corrections.build(user: current_user)
If your corrections model has_many :users, through: :posts
, then the following should work: 如果您的更正模型
has_many :users, through: :posts
,则以下内容应该有效:
@post.corrections.build(users: [current_user])
EDIT 编辑
The format of your form_for is also incorrect. form_for的格式也不正确。 The
url:
key in form_for
doesn't belong in the resource array (the first arg in your form_for) form_for
中的url:
key不属于资源数组(form_for中的第一个arg)
The following modification should resolve the error, provided user_post_corrections_path
takes :id
, :post_id
, and :correction_id
如果
user_post_corrections_path
采用以下修改,则应解决该错误:id
, :post_id
和:correction_id
form_for @correction, url: user_post_corrections_path(current_user, @post, @correction), html: { multipart: true} do |f|
I'd also reduce your routing to two levels of nesting if possible. 如果可能的话,我还会将您的路由减少到两个嵌套级别。
Perhaps this is easier? 也许这更容易?
resources :posts do
resources :corrections
end
end
If it's implied a post belongs to the current_user
then it may not be necessary to have /users
in the route path. 如果暗示帖子属于
current_user
则可能没有必要在路径路径中包含/users
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.