Rails Rails 参数传递问题

runup · 2016年03月12日 · 最后由 runup 回复于 2016年03月15日 · 3755 次阅读

参考episode 57:create model through text field,自己进行实现,这段视频要实现的是在 product 中添加 tag。但是自己在处理的过程中总是会出现 category_id=nil,就是说 view 中的 new_category_name 传不到 model 中。

自己的代码已经上传到github

详细代码如下:

rails g scaffold category name:string
rails g scaffold product name:string content:text category:references

#model中
class Product < ActiveRecord::Base
  belongs_to :category
  attr_accessor :new_category_name
  before_save :create_category_from_name

  def create_category_from_name
    create_category(name: new_category_name) unless new_category_name.blank?
  end
end

#controller中
def create
  @product = Product.new(product_params)

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

#view中
<div class="field">
  <%= f.collection_select(:category_id, Category.all, :id, :name, prompt:     "请选择")%>
  <%= f.text_field :new_category_name %>
</div>

根据各位的指点,通过在 strong parameter 添加相关参数解决了这个问题,同时在实现另外一个 episode 的时候又遇到了问题,问题如下所示:

参考的是17 HABTM Checkboxes (revised),自己进行了实现,这是我提交到 github 中的代码

场景是为一个 model 添加多个可选 tag

上一篇的论述中,是因为没有将需要传递的参数添加 strong parameter 中,因次参数被过滤。这次考虑了强参,但是因为遇到了不熟悉的 api,因此试了几次,都没有成功,代码如下:

rails g scaffold product name:string content:text
rails g scaffold category name:string
rails g scaffold categorization product_id:integer category_id:integer 
#model
class Product < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, through: :categorizations
end

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :products, through: :categorizations
end

class Categorization < ActiveRecord::Base
  belongs_to :product
  belongs_to :category
end

#controller
def product_params
  #不能确定使用category_id是否合适
  params.require(:product).permit(:name, :content, :category_id)
end

#view
<div class = "field">
  <%= hidden_field_tag "product[category_ids][]", nil %>
  <% Category.all.each do |category| %>
  <%= check_box_tag "product[category_ids][]", category.id, @product.category_ids.include?(category.id) %>
  <%= category.name %></br>
  <% end %>
</div>

麻烦提供下 controller 里的代码,不然不好说得。

#1 楼 @qinfanpeng 已经添加,controller 的代码是支架生成的,所以之前就没有贴出来

你没有把 new_category_name 加到 strong_params 中https://github.com/Jayzen/add_tag/blob/master/app/controllers/products_controller.rb#L70-L73.

在 console 里你应该看到那个 custom field 了,但是它被 filtered 了

估计是没把字段写入 product_params 里面去

#2 楼 @runup 参照 3,4 两楼的来弄

#4 楼 @killernova #3 楼 @jiazhen #5 楼 @qinfanpeng 应该是这个问题,代码检查了很多遍,都没有发现什么问题。rails 2 的时候还没有 strong params

#6 楼 @runup 你这不像是 Rails 2 的代码撒,还是说你用过 Rails 2,近来没怎么用过 Rails 了?

#7 楼 @qinfanpeng 这个 episode 的代码是专门针对 rails2 的吧,所以在进行模仿的时候就没有想过强参数的问题,强参数应该是 3.2 才有吧。

#7 楼 @qinfanpeng 我是一个初学者,以前自学过一段时间,然后停止了,今年又重新拿起来,准备一直专研下去,谢谢一直的答疑解惑,对我帮助很大,感恩。

#9 楼 @runup 无需客气,不过建议找个新些的资料,至少要是 Rails 4 的吧,不然后面会有些许困惑。《Rails Tutorial》应该会新些。

#10 楼 @qinfanpeng 最新的 tutorial 已经练习过几遍,railscasts 中的东西还蛮实用的。

#4 楼 @killernova #3 楼 @jiazhen #1 楼 @qinfanpeng 对问题进行了更新,应用 strong parameter,解决了昨天提出的问题,但是在一个新的场景 中又出现了问题,关键是我自己不清楚,在这个场景中,如果选择 strong parameter,问题中 category_id 是否有问题?

params.require(:product).permit(:name, :content, {category_ids: []})

#13 楼 @qinfanpeng 测试成功,但是能提供一些依据或者其他参考么?

#14 楼 @runup Goole Keywords: Rails strong parameters permit array

前面几条都可以,比如这个 http://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters

感觉你需要系统地学下 Rails,不然会有不少小烦忧。

感谢。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号