2017-06-12 58 views
1

我需要一个应用程序使用嵌套的路线和assocciations,如:为...未知属性上构建命令

/char_type/:param1/char_attr/:param2 

我用宝石nested_scaffold产生了我的一切。

我结束了与车型:

class CharacterType < ApplicationRecord 
    mount_uploader :avatar, AvatarUploader 
    has_many :character_attribute 
end 

class CharacterAttribute < ApplicationRecord 
    belongs_to :character_types 
end 

迁移:

class CreateCharTypes < ActiveRecord::Migration[5.0] 
    def change 
    create_table :char_types do |t| 
     t.string :name 
     t.string :avatar 

     t.timestamps 
    end 
    end 
end 

class CreateCharAttrs < ActiveRecord::Migration[5.0] 
def change 
    create_table :char_attrs do |t| 
     t.references :char_types, foreign_key: true 
     t.string :name 
     t.integer :value 
     t.string :avatar 

     t.timestamps 
    end 
    end 
end 

CharacterTypesController:

class CharacterTypesController < ApplicationController 
    before_action :set_character_type, only: [:show, :edit, :update, :destroy] 

    def index 
    @character_types = CharacterType.all 
    end 

    def show 
    end 

    def new 
    @character_type = CharacterType.new 
    end 

    def edit 
    end 

    def create 
    @character_type = CharacterType.new(character_type_params) 

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

    def update 
    respond_to do |format| 
     if @character_type.update(character_type_params) 
     format.html { redirect_to @character_type, notice: 'Character type was successfully updated.' } 
     format.json { render :show, status: :ok, location: @character_type } 
     else 
     format.html { render :edit } 
     format.json { render json: @character_type.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @character_type.destroy 
    respond_to do |format| 
     format.html { redirect_to character_types_url, notice: 'Character type was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_character_type 
     @character_type = CharacterType.find(params[:id]) 
    end 

    def character_type_params 
     params.require(:character_type).permit(:name, :avatar) 
    end 
end 

而且CharacterAttributesController:

class CharacterAttributesController < ApplicationController 
    before_action :set_character_attributes 
    before_action :set_character_attribute, only: [:show, :edit, :update, :destroy] 

    def index 
    @character_attributes = @character_types.character_attributes 
    end 

    def show 
    end 

    def new 
    @character_attribute = @character_types.character_attribute.build 
    end 

    def edit 
    end 

    def create 
    @character_attribute = @character_types.character_attributes.build(character_attribute_params) 

    if @character_attribute.save 
     redirect_to([@character_attribute.character_types, @character_attribute], notice: 'Character attribute was successfully created.') 
    else 
     render action: 'new' 
    end 
    end 

    def update 
    if @character_attribute.update_attributes(character_attribute_params) 
     redirect_to([@character_attribute.character_types, @character_attribute], notice: 'Character attribute was successfully updated.') 
    else 
     render action: 'edit' 
    end 
    end 

    def destroy 
    @character_attribute.destroy 

    redirect_to character_types_character_attributes_url(@character_types) 
    end 

    private 

    def set_character_attributes 
     @character_types = CharacterType.find(params[:character_type_id].to_i) 
    end 

    def set_character_attribute 
     puts("im entering here:1") 
     puts("params is:1") 
     puts(params[:character_type_id].inspect) 
     @character_attribute = @character_types.character_attributes.find(character_types_id: params[:character_type_id].to_i) 
    end 

    def character_attribute_params 
     params.require(:character_attribute).permit(:name, :value, :avatar) 
    end 
end 

和视图形式新char_attr

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

     <ul> 
     <% @character_attribute.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 :value %><br /> 
    <%= f.number_field :value %> 
    </div> 
    <div class="field"> 
    <%= f.label :avatar %><br /> 
    <%= f.text_field :avatar %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

我在哪里得到我的错误是在CharacterAttributeController当我尝试通过功能new创建现有CharacterType新CharacterAttribute。

我把它从我的观点一样:

new_character_type_character_attribute_path(character_type_id: character_type[:id]) 

而我得到的错误:

ActiveModel::UnknownAttributeError in CharAttrsController#new 
unknown attribute 'char_type_id' for CharAttr. 

# GET char_types/1/char_attrs/new 
    def new 
    @char_attr = @char_types.char_attrs.build 
    end 

# GET char_types/1/char_attrs/1/edit 

Request 
Parameters:  
{"char_type_id"=>"1"} 
+0

有一种形式或从那里你一个新的对象发送值的看法? –

+0

是的,控制器中的“新”功能不应该先被调用吗?实例化一个空对象?我用视图形式更新问题。 –

+0

是的,我的意思是,为了知道'char_type_id'从哪里来,它可以是'character_type_id'或'character_types_id'吗? –

回答

1

如果您需要引用不同的表,你可以将其设置使用

class CharacterAttribute < ApplicationRecord 
    self.table_name = 'char_attrs' 
    belongs_to :character_type 
end 

默认情况下,rails会查找character_attributes表,该表不存在。

那么你可以做,

@char_type.character_attributes.build() 
+1

t。引用:char_types,foreign_key:true belongs_to:character_types 他也因为某些原因使用了复数形式 –