form_for a" belongs_to"模特儿在' show'父模型

时间:2015-07-15 06:42:28

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个型号"面包" has_many"帖子"。 我想要一个表格来创建一个新的" Post"在' show'给定的页面"面包"这创造了与面包'记录的联系。其中'显示'页面正在显示。

我尝试过几种不同的方法,但都是错误的。我在下面显示的方法给出了一个"关联不能用于与对象无关的表单中。错误。

/views/breads/show.html.erb:

@If(Model==null || Model.RoomsAvailable.Count<1)
{
//Don't read c# code on rest of the view now
}

配置/ routes.rb中

<p>
  <strong>Bread Type:</strong>
  <%= @bread.bread_type %>
</p>

<table>
  <tr>
    <th>Uploaded By</th>
    <th>Comment</th>
    <th>Picture</th>
  </tr>
<% @bread.posts.each do |post| %>
  <tr>
    <td><%= post.uploader %></td>
    <td><%= post.comment %></td>
    <td><%= image_tag post.attachment_url.to_s %></td>
  </tr>

<% end %>
</table>

<%= @bread.id %>

<%= simple_form_for @bread do |b| %>
  <%= simple_fields_for :posts do |p| %>
    <%= p.input :uploader %>
    <%= p.input :comment %>
    <%= p.association :bread, value: @bread.id %>
    <%= p.file_field :attachment %><br>
    <%= p.button :submit %>
  <% end %>
<% end %>

<%= link_to 'Back', breads_path %>

控制器/ breads_controller.rb:

Rails.application.routes.draw do
  get 'welcome/index'

  root 'welcome#index'

  resources :breads

  resources :posts
end

模型/ bread.rb:

class BreadsController < ApplicationController
  def index
    @breads = Bread.all
  end

  def show
    @bread = Bread.find(params[:id])
  end

  def new
    @bread = Bread.new
  end

  def edit
    @bread = Bread.find(params[:id])
  end

  def create
    @bread = Bread.new(bread_params)

    if @bread.save
      redirect_to @bread
    else
      render 'new'
    end
  end

  def update
    @bread = Bread.find(params[:id])

    if @bread.update(bread_params)
      redirect_to @bread
    else
      render 'edit'
    end
  end

  def destroy
    @bread = Bread.find(params[:id])
    @bread.destroy

    redirect_to breads_path
  end

  private
    def bread_params
      params.require(:bread).permit(:bread_type)
    end
end

模型/ post.rb:

class Bread < ActiveRecord::Base
  has_many :posts
  validates :bread_type,  presence: true, uniqueness: true
end

3 个答案:

答案 0 :(得分:0)

这样做 -

  <%= simple_form_for @bread do |b| %>
    <%= b.simple_fields_for(:posts,@bread.posts.build) do |p| %>
      <%= p.input :uploader %>
      <%= p.input :comment %>
      <%= p.file_field :attachment %><br>
      <%= p.button :submit %>
    <% end %>
  <% end %>

并在beard_params中进行更改

 def beard_params
   params.require(:bread).permit!
 end

此处permit!需要所有参数,而其他方式则可以使用@ pawan的答案。

答案 1 :(得分:0)

扩展 @Amit Suroliya 回答,您需要将posts_attributes添加到bread_params

def bread_params
   params.require(:bread).permit(:id, :bread_type, posts_attributes: [:id, :uploader, :comment, :bread_id, :attachment])
end

<强>更新

您还需要在 面包 模型中添加accepts_nested_attributes_for :posts

答案 2 :(得分:0)

对不起,但这根本不是好办法,试着不要滥用铁轨和休息路线:)

以下是如何做到这一点的简单示例:

<强>配置/ routes.rb中

resources :bread do 
  resources :posts
end

这意味着会有以下路线:

bin/rake routes

breads - breads#index
bread/:id - breads#show
etc..
and most important
bread/:bread_id/posts/:id
...

这意味着帖子是面包的嵌套资源......

应用/控制器/ breads_controller.rb

controller BreadsController < BaseController
   before_action :find_bread, except: %i(index create new)

   .... action new, update, edit etc..
end

但现在它是PostsController中的重要部分..

应用/控制器/ posts_controller.rb

controller PostsController < BaseController
   before_action :find_bread
   before_action :find_post, except: %i(index new create)
   before_action :build_post, only: %i(new create)

   .... action new, update, edit etc..

   # Example with :return link 
   def create
    if @post.save
       if params[:back] == 'bread_show'
         redirect_to bread_path(@bread)
       else
         redirect_to bread_post_path(@bread, @post)
       end
     else
       render 'new'
     end
   end

   private

   def build_post
      if params[:post]
         @post = @bread.posts.build(post_params)
      else
         @post = @bread.posts.build
      end
   end

   def find_post
     @post = @bread.posts.find(params[:id])
   end

   def find_bread
     @bread = Bread.find(params[:bread_id])
   end

   ... post params ...
end

现在你有完整的路线休息,你可以做你想做的事,没有这么痛苦和干净

... output hidden

<%= @bread.id %>

<%= simple_form_for @bread.posts.build do |b| %>
  <%= p.input :uploader %>
  <%= p.input :comment %>
  <%= p.file_field :attachment %><br>
  <%# Send back link to return on proper page %>
  <%= p.hidden_field :back, 'bread_show' %>
  <%= p.button :submit %>
<% end %>

<%= link_to 'Back', breads_path %>

可能会有一些错误,我从内存中编写此代码,不能尝试:(