-1
這是我new.html.erb更新記錄,如果存在其他創造新紀錄的軌道
<%= form_for @post do |f| %>
<%= f.text_field :title,placeholder: 'title' %>
<%= f.hidden_field :gen_id,value: 55 %> #55 is just for testing
<%= f.submit 'Submit' %>
<% end %>
這是我的控制器
def create
@post = Post.find_by_id params[:gen_id]
if @post
@post.update(post_params)
else
@post = Post.new
if @post.save!
flash[:success] = 'Post added successfully'
redirect_to action: :new
else
flash[:error] = 'Post cannot add. Please try again after some time'
redirect_to action: :new
end
end
end
上面的代碼只是創造新的記錄,每次
你在軌道3使用強PARAMS? – Santhosh
@acts_as_geek我正在使用rails 4 – overflow
有一個'find_or_create_by(...)'方法 – j03w