在Rails错误中通过Ajax提交表单

时间:2022-05-04 20:11:48

I want to submit some form via ajax call and I have written the following code but I am getting the error undefined method `model_name' for Hash:Class

我想通过ajax调用提交一些表单,我已经编写了以下代码但是我收到错误未定义方法`model_name'用于Hash:Class

  <%= form_for :url=>articles_editcomment_path ,:method=>:post ,:remote => true do |f| %>

      <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
      </p>
      <p>
        <%= f.submit %>
      </p>
    <% end %>

how do I overcome the error?

我该如何克服错误?

2 个解决方案

#1


0  

You are not mentioning the model or variable for form:

您没有提到表单的模型或变量:

<%= form_for :article, :url=>articles_editcomment_path ,:method=>:post ,:remote => true do |f| %>

Above method will not require a variable of Article type and will not populate your form in editing mode.

上述方法不需要文章类型的变量,也不会在编辑模式下填充表单。

OR

<%= form_for @article, :url=>articles_editcomment_path ,:method=>:post ,:remote => true do |f| %>

Above method will require a variable named @article which will may be initialized in controller and will populate the form in editing mode.

上面的方法将需要一个名为@article的变量,它可以在控制器中初始化,并在编辑模式下填充表单。

If this doesn't solve the problem feel free to ask for more

如果这不能解决问题,请随时提出要求

#2


0  

form_for

The problem you have is you're using form_for and not passing an ActiveRecord object - which is why your error is appearing as follows:

你遇到的问题是你正在使用form_for而不是传递一个ActiveRecord对象 - 这就是为什么你的错误出现如下:

undefined method `model_name' for Hash:Class

用于Hash的未定义方法`model_name':Class

Notice what you're passing to the form_for method - a hash.

注意你传递给form_for方法的内容 - 一个哈希。

--

What you should do is populate the form with an ActiveRecord object. This is how Rails builds the ActiveRecord forms with form_for -- it takes data from the object itself & then builds the form off the back of it

您应该做的是使用ActiveRecord对象填充表单。这就是Rails使用form_for构建ActiveRecord表单的方式 - 它从对象本身获取数据然后在其后面构建表单

So to fix your problem, you need to populate your form with an ActiveRecord object:

因此,要解决您的问题,您需要使用ActiveRecord对象填充表单:

#app/controllers/comments_controller.rb
Class CommentsController < ApplicationController
   def edit
       @article = Article.find params[:article_id]
       @comment = Comment.find params[:id]
   end
end

#app/views/comments/edit.html.erb
<%= form_for [@article, @comment] do |f| %>
   ...
<% end %>

#1


0  

You are not mentioning the model or variable for form:

您没有提到表单的模型或变量:

<%= form_for :article, :url=>articles_editcomment_path ,:method=>:post ,:remote => true do |f| %>

Above method will not require a variable of Article type and will not populate your form in editing mode.

上述方法不需要文章类型的变量,也不会在编辑模式下填充表单。

OR

<%= form_for @article, :url=>articles_editcomment_path ,:method=>:post ,:remote => true do |f| %>

Above method will require a variable named @article which will may be initialized in controller and will populate the form in editing mode.

上面的方法将需要一个名为@article的变量,它可以在控制器中初始化,并在编辑模式下填充表单。

If this doesn't solve the problem feel free to ask for more

如果这不能解决问题,请随时提出要求

#2


0  

form_for

The problem you have is you're using form_for and not passing an ActiveRecord object - which is why your error is appearing as follows:

你遇到的问题是你正在使用form_for而不是传递一个ActiveRecord对象 - 这就是为什么你的错误出现如下:

undefined method `model_name' for Hash:Class

用于Hash的未定义方法`model_name':Class

Notice what you're passing to the form_for method - a hash.

注意你传递给form_for方法的内容 - 一个哈希。

--

What you should do is populate the form with an ActiveRecord object. This is how Rails builds the ActiveRecord forms with form_for -- it takes data from the object itself & then builds the form off the back of it

您应该做的是使用ActiveRecord对象填充表单。这就是Rails使用form_for构建ActiveRecord表单的方式 - 它从对象本身获取数据然后在其后面构建表单

So to fix your problem, you need to populate your form with an ActiveRecord object:

因此,要解决您的问题,您需要使用ActiveRecord对象填充表单:

#app/controllers/comments_controller.rb
Class CommentsController < ApplicationController
   def edit
       @article = Article.find params[:article_id]
       @comment = Comment.find params[:id]
   end
end

#app/views/comments/edit.html.erb
<%= form_for [@article, @comment] do |f| %>
   ...
<% end %>