如何从部分Rails 6创建嵌套模型 [英] How to create nested model from partial Rails 6

查看:47
本文介绍了如何从部分Rails 6创建嵌套模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手在这里!

我尝试提交嵌套模型的形式失败.

目标:

通过将布尔值提交给 reviews 模型以将翻译是否已通过翻译 entry user 相关联来验证翻译与参考相关的模型.

 #routes.rb资源:条目资源:评论结尾#entry.rb当属:用户has_one:评论accepts_nested_attributes_for:审查#user.rbhas_many:条目has_many:评论#review.rb当属:用户当属:进入 

entry 索引中,将 entry 实例传递给partial,可以完美地工作

 #/entries/index.html.erb<%@ entries.each做| entry |%>...<%=呈现评论/新",条目:条目%>...<%结束%> 

未成功设置新表格/创建表格.发生的情况是 entry 实例受到好评,但是我无法为属于 entry review 创建新的模型实例.

entry.review review 引发 nil 错误,而通过浏览器控制台进行测试时, entry 很好

格式中的第一个参数不能包含nil或为空

 #reviews/_new.html.erb< span><%= form_for entry.review做| f |%>< div class ="form-check form-switch"><%= f.check_box:已验证,类别:"form-check-input"%></div><%= f.提交类别:"btn btn-primary"%><%结束%></span> 

另一种尝试是仅使用控制器中的 @review ,但不遵循嵌套路由.

我的控制器看起来像这样

 #reviews_controller.rbdef创建@entry = Entry.find(params [:entry_id])@review = @ entry.review.build(review_params)@ review.user_id = current_user.id@ review.save结尾私人的def review_paramsparams.require(:review).permit(:verified,用户:current_user,条目:@entry)结尾 

我想在 entries_controller 中实现我的动作吗?

我还找到了这里有用,但是复制失败.

另一个StackOverflow参考此处

我仍然收到错误 entry.review.build ,该评论为零.

格式中的第一个参数不能包含nil或为空

解决方案

在通过 has_one 关系而不是

关系构建关联记录时

 <代码> @review = entry.review.build(review_params) 

您需要使用以下内容:

 <代码> @review = entry.build_review(review_params) 

请参见文档应该可以帮助您入门.

Newbie Here!

I'm unsuccessfully trying to submit a form of a nested model.

Aim:

Verify a translation by submitting a boolean to a reviews model to associate whether a translation is verified or not, with translation entry and user model associated references.

# routes.rb
resources :entries do
   resources :reviews
end


# entry.rb
belongs_to :user
has_one :review
accepts_nested_attributes_for :review


# user.rb
has_many :entries
has_many :reviews


# review.rb
belongs_to :user
belongs_to :entry

From entry index, pass the entry instance to partial, works perfect

# /entries/index.html.erb
<% @entries.each do |entry| %>
   ...
   <%= render 'reviews/new', entry: entry %>
   ...
<% end %>

Unsuccessfully setting up new/create form. What happens is that the entry instance is well received, but I am failing to create a new model instance for review belonging to entry.

entry.review raises an nil error for review, while entry is fine when testing through browser console

First argument in form cannot contain nil or be empty

# reviews/_new.html.erb
<span>
  <%= form_for entry.review do |f| %>
    <div class="form-check form-switch">
      <%= f.check_box :verified, class: "form-check-input" %>
    </div>
    <%= f.submit class: "btn btn-primary"%>
  <% end %>
</span>

Another attempt was also to use just @review from the controller but that doesn't obey nested routes.

My controller looks like this

# reviews_controller.rb

def create
    @entry = Entry.find(params[:entry_id])
    @review = @entry.review.build(review_params)
    @review.user_id = current_user.id
    @review.save
end

private

def review_params
   params.require(:review).permit(:verified, user: current_user, entry: @entry)
end

Am I suppose to implement my actions in the entries_controller?

I have also found the tutorial here useful but replication was unsuccessful.

Another StackOverflow reference here

I still get the error entry.review.build that review is nil.

First argument in form cannot contain nil or be empty

解决方案

When building an associated record over a has_one relation, instead of

@review = entry.review.build(review_params)

you need to use the following:

@review = entry.build_review(review_params)

See the documentation for more details.

Am I suppose to implement my actions in the entries_controller?

It depends on what you're after. If you have a dedicated form for adding a new review and it is not embedded in another form for creating or updating an entry then implementing a create action in ReviewsController is the straightforward solution – in this case you should also not need accepts_nested_attributes_for in Entry.

If, however, you want to be able to create or update an entry as well as its review using the same form, then you should nest the review form in the form of the entry, keep accepts_nested_attributes_for, and use actions in EntriesController. The documentation should get you started there.

这篇关于如何从部分Rails 6创建嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆