2011-12-26 33 views
-1

我有以下关系:我的编辑操作使用创建行动,而不是更新

store.rb -> has_many :products 
product.rb -> belongs_to :store 

的routes.rb

resources :stores do 
    resources :products 
end 

builds_controller.rb

def edit 
    @build = Build.find(params[:id]) 
    @user = User.find(@build.user_id) 
    @hero = Hero.find(@build.hero_id) 

    @heros = Hero.order('name ASC') 
    @items = Item.order('name ASC') 

    unless current_user.id == @user.id 
    respond_to do |format| 
     format.html { redirect_to root_path, notice: 'You are only allowed to edit your own builds' } 
    end 
    end 
end 

出于某种原因,每当我尝试去构建编辑页面并尝试编辑它时,它会运行创建而不是更新

任何人都知道这可能是什么原因?

此外,我想编辑页面上的表单填写与构建的当前数据。我如何实现这一目标?

我的回购:https://github.com/imjp/DotA-Items

回答

1

你的问题来自于您的形式:

​​

它需要

<%= semantic_form_for([@user, @build]) do |f| %> 

现在,在new行动,您还需要准备所需变量:

@user = User.find(params[:user_id]) #assuming you have a path like users/id/builds/new 
# or @user = current_user if that's what you want 
@build = @user.builds.build 

PS:你应该使用cancan宝石来管理,而不是你的回应做的东西像if @user.id == current_user.id

+0

感谢授权。它仍然在创建新版本而不是更新版本:/ – imjp 2011-12-26 23:31:24

+1

当您进入版本的编辑页面时,输入内容会填充版本信息? – Robin 2011-12-26 23:47:15

+0

它现在工作!谢啦。但是,当我进入编辑操作时,它仍然没有填充字段。有什么建议么? – imjp 2011-12-26 23:59:47

相关问题