我已经创建了一个与职责一对多关联的项目脚手架。我可以呈现责任表单,但我不能将project_id设置为责任表。我已经创建了一对多关联。
这是我的代码-
routes.rb
resources :projects do
resources :responsibilities
end责任form.html.erb
<%= form_with(model: responsibility, url: [@project, responsibility], local: true) do |form| %>
<% if responsibility.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(responsibility.errors.count, "error") %> prohibited this responsibility from being saved:</h2>
<ul>
<% responsibility.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :responsibility_matrix %>
<%= form.text_field :responsibility_matrix %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>responsibilities_controller.rb
def new
@project = Project.find(params[:project_id])
@responsibility = Responsibility.new
end发布于 2020-02-04 13:07:48
循序渐进
新应用
rails new bookstore添加脚手架作者
rails g scaffold author name添加脚手架书籍
rails g scaffold book name author:references更新路由
从…
resources :books
resources :authors至
resources :authors do
resources :books
end让我们在app/views/authors/show.html.erb中显示新书链接,添加
<%= link_to 'New book', new_author_book_path(@author) %> |在创建了第一个作者并访问了http://localhost:3000/authors/1/books/new之后,我们有了一个错误: Books#new中的NoMethodError
undefined method `books_path'要进行修复,请首先在BooksController中添加
before_action :set_author, only: [:new]
private
def set_author
@author = Author.find(params[:author_id])
end在app/views/book/_form.html.erb中
<%= form_with(model: book, url:[@author, book], local: true) do |form| %>再次访问http://localhost:3000/authors/1/books/new
Books#new中的NameError
undefined local variable or method `books_path'修复app/views/book/new.html.erb
变化
<%= link_to 'Back', books_path %>至
<%= link_to 'Back', author_books_path(@author) %>现在我们可以呈现http://localhost:3000/authors/1/books/new了
我想你在这里得到了你需要的一切
https://stackoverflow.com/questions/60050471
复制相似问题