Rails 4嵌套资源/路由......差不多......?

时间:2014-03-16 23:14:53

标签: ruby-on-rails ruby ruby-on-rails-4

我有三种型号 - 用户,制造商和生产线。我需要用户拥有多个制造商,每个制造商都有多条生产线。我终于将行最终拉到_form上的manufacturer_id,但现在它没有保存'line'的'name',只保存id。它会将'name'保存为nil,就像它没有从表单中传递名称一样。我已经阅读了关于此的标准文档,我不能为我的生活让它工作。它一直挂在控制器的最后创建阶段,我感觉我在窗体和控制器的路径上有很多错误。希望有人能够轻松对待我并帮助我让整个shebang工作。

user.rb

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :manufacturers
end

manufacturer.rb

class Manufacturer < ActiveRecord::Base
  belongs_to :user
  has_many :lines
end

line.rb

class Line < ActiveRecord::Base
  belongs_to :manufacturer
end

lines_controller.rb

class LinesController < ApplicationController
  before_action :set_line, only: [:show, :edit, :update, :destroy]
  before_filter :load_manufacturer

  # GET /lines
  # GET /lines.json
  def index
    @lines = Line.all
  end

  # GET /lines/1
  # GET /lines/1.json
  def show
  end

  # GET /lines/new
  def new
    @manufacturer = Manufacturer.find(params[:manufacturer_id])
    @line = @manufacturer.lines.build
  end

  # GET /lines/1/edit
  def edit
  end

  # POST /lines
  # POST /lines.json
  def create
    @line = @manufacturer.lines.build

    respond_to do |format|
      if @line.save
        format.html { redirect_to @line, notice: 'Line was successfully created.' }
        format.json { render action: 'show', status: :created, location: @line }
      else
        format.html { render action: 'new' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /lines/1
  # PATCH/PUT /lines/1.json
  def update
    respond_to do |format|
      if @line.update(line_params)
        format.html { redirect_to @line, notice: 'Line was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /lines/1
  # DELETE /lines/1.json
  def destroy
    @line.destroy
    respond_to do |format|
      format.html { redirect_to lines_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_line
      @line = Line.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_params
      params.require(:line).permit(:name, :manufacturer_id)
    end

    def load_manufacturer
      @manufacturer = Manufacturer.find(params[:manufacturer_id])
    end
end

行/ _form.html.erb

<%= form_for [@manufacturer,@line] do |f| %>
  <% if @line.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@line.errors.count, "error") %> prohibited this line from being saved:</h2>

      <ul>
      <% @line.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :manufacturer_id %><br>
    <%= f.text_field :manufacturer_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

的routes.rb

Linecarder::Application.routes.draw do
  resources :manufacturers do
    resources :lines
  end

  devise_for :users
  root "pages#home"

  get "about" => "pages#about"

  match 'users/:id' => 'users#show', :via => "get"
  match '/users', :to => 'users#index', :as => "all_users", :via => "get"
  match '/users/:name' => 'users#show', via: :get, as: :public_profile
end

rake routes

                  Prefix Verb   URI Pattern                                              Controller#Action
      manufacturer_lines GET    /manufacturers/:manufacturer_id/lines(.:format)          lines#index
                         POST   /manufacturers/:manufacturer_id/lines(.:format)          lines#create
   new_manufacturer_line GET    /manufacturers/:manufacturer_id/lines/new(.:format)      lines#new
  edit_manufacturer_line GET    /manufacturers/:manufacturer_id/lines/:id/edit(.:format) lines#edit
       manufacturer_line GET    /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#show
                         PATCH  /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#update
                         PUT    /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#update
                         DELETE /manufacturers/:manufacturer_id/lines/:id(.:format)      lines#destroy
           manufacturers GET    /manufacturers(.:format)                                 manufacturers#index
                         POST   /manufacturers(.:format)                                 manufacturers#create
        new_manufacturer GET    /manufacturers/new(.:format)                             manufacturers#new
       edit_manufacturer GET    /manufacturers/:id/edit(.:format)                        manufacturers#edit
            manufacturer GET    /manufacturers/:id(.:format)                             manufacturers#show
                         PATCH  /manufacturers/:id(.:format)                             manufacturers#update
                         PUT    /manufacturers/:id(.:format)                             manufacturers#update
                         DELETE /manufacturers/:id(.:format)                             manufacturers#destroy
        new_user_session GET    /users/sign_in(.:format)                                 devise/sessions#new
            user_session POST   /users/sign_in(.:format)                                 devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                                devise/sessions#destroy
           user_password POST   /users/password(.:format)                                devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                            devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                           devise/passwords#edit
                         PATCH  /users/password(.:format)                                devise/passwords#update
                         PUT    /users/password(.:format)                                devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                                  devise/registrations#cancel
       user_registration POST   /users(.:format)                                         devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                                 devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                    devise/registrations#edit
                         PATCH  /users(.:format)                                         devise/registrations#update
                         PUT    /users(.:format)                                         devise/registrations#update
                         DELETE /users(.:format)                                         devise/registrations#destroy
                    root GET    /                                                        pages#home
                   about GET    /about(.:format)                                         pages#about
                         GET    /users/:id(.:format)                                     users#show
               all_users GET    /users(.:format)                                         users#index
          public_profile GET    /users/:name(.:format)                                   users#show

1 个答案:

答案 0 :(得分:3)

更新create操作,如下所示:

 def create
    @line = @manufacturer.lines.build(line_params) ## Pass the params via line_params

    respond_to do |format|
      if @line.save
        format.html { redirect_to @line, notice: 'Line was successfully created.' }
        format.json { render action: 'show', status: :created, location: @line }
      else
        format.html { render action: 'new' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

您没有传递从表单收到的参数。因此,name行不会保存在数据库中。 此外,为了允许质量分配属性(强参数)通过line_params方法传递它们。

更新,以解决要解决的评论中的OP问题 undefined method 'line_url'... for 'format.html { redirect_to @line, notice: 'Line was successfully created.' }'

使用此

format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully created.' }

而不是

format.html { redirect_to @line, notice: 'Line was successfully created.' }