2014-03-06 33 views
0

我有一个formfield在我的形式是这样的:轨将不保存表单字段

<div class="tags"> 
    <%= f.label :tags %> 
    <%= f.text_area :tags %> 
</div> 

我跑这样的迁移:

class AddTagsToIssues < ActiveRecord::Migration 
    def change 
    add_column :issues, :tags, :text 
    end 
end 

当我保存,新行被添加到DB,但标签=零,尽管我打字有点像在德text_area“测试”

在我的开发

登录我:

未经许可参数:标签

我已经试过白名单在我的控制器:

def create 
    @issue = Issue.new(issue_params) 
    Issue.new(params.permit(:tags)) 

但这不起作用。

更新跟进的问题:

全力打造方法:

def create 
    def issue_params  
    params.require(:issue).permit(:tags) 
    end 
    @issue = Issue.new(issue_params) 
    Issue.new(params.permit(:tags)) 
    respond_to do |format| 
     if @issue.save 
     format.html { redirect_to @issue, notice: 'Issue was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @issue } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @issue.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

型号:

class Issue < ActiveRecord::Base 

    belongs_to :project 

end 

表创建查询:

class CreateIssues < ActiveRecord::Migration 
    def change 
    create_table :issues do |t| 
     t.string :title 
     t.text :description 
     t.integer :no_followers 

     t.timestamps 
    end 
    end 
end 

所以我没有pe这种问题只发生在稍后添加标签,然后仅与标签一起发生。

回答

4

tags正在设置为nil,因为您不允许它。

tags许可证在如下的方法issue_params

def issue_params  
    params.require(:issue).permit(:tags,...) 
end 

其中,...指其它字段在模型Issue

create行动应该是这个样子,

def create 
    @issue = Issue.new(issue_params) ## issue_params called 
    if @issue.save      ## save the record 
     redirect_to @issue, notice: 'Issue was successfully created.' 
    else 
     render action: 'new' 
    end 
    end 
+0

谢谢!后续问题:为什么我必须为这个新的参数做这件事,而其余的东西却被保存了呢? – matthijs

+0

其余的正在保存,因为你已经允许其余的参数。如果你不允许'标签'它永远不会被保存。 –

+0

但是我怎样才允许其他参数?我的代码现在更新为:params.require(:issue).permit(:tags)。请注意,我在这里不允许使用其他参数。它们仍然像标签一样保存,并在迁移前保存:add_column:issues,:tags,:text – matthijs