我的表格很简单:
<%= form_for(@shop, remote: true, data: { confirm: "You sure?"}) do |fa| %>
<%= fa.hidden_field :status, :value => 1 %>
<%= fa.submit "Approve", class: "btn btn-large btn-success" %>
<% end %>
<%= form_for(@shop, remote: true, data: { confirm: "You sure?" }) do |fr| %>
<%= fr.hidden_field :status, :value => 2 %>
<%= fr.submit "Reject", class: "btn btn-large btn-danger" %>
<% end %>
我想更新status
的属性@shop
。按&#34;批准&#34;应该将其设置为1,&#34;拒绝&#34; - 分别为2。但是,当我点击按钮(确认除外)时,没有任何反应。我知道我的代码中没有重要且基本的东西,但我无法弄清楚是什么。
另外,有没有办法在一个form_for中写入所有内容?像
<%= form_for(@shop, remote: true, data: { confirm: "You sure?"}) do |fa| %>
<%= fa.hidden_field :status_a, :value => 1 %>
<%= fa.hidden_field :status_r, :value => 2 %>
<%= fa.submit "Approve", class: "btn btn-large btn-success" %>
<%= fa.submit "Reject", class: "btn btn-large btn-danger" %>
<% end %>
PS:商店控制器有:
def update
if @shop.update_attributes(shop_params)
flash[:success] = "Shop updated"
redirect_to @shop
else
render 'edit'
end
end
def edit
end
.
.
private
def shop_params
params.require(:shop).permit(:title, :rating, :pic, :status,
address_attributes: [:country, :state, :city, :street])
end
按批准按钮时服务器日志:
Started PATCH "/shops/64" for 127.0.0.1 at 2014-08-12 12:52:36 +0400
Processing by ShopsController#update as JS
Parameters: {"utf8"=>"✓", "shop"=>{"status"=>"1"}, "commit"=>"Approve", "id"=>"64"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '5927a9415e68e7b293f8d1e24a709961bc260195' LIMIT 1
Completed 500 Internal Server Error in 18ms
NoMethodError (undefined method `update_attributes' for nil:NilClass):
app/controllers/shops_controller.rb:35:in `update'
Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.0ms)
Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.1ms)
Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (66.5ms)
ShopsController中的第35行是
if @shop.update_attributes(shop_params)
答案 0 :(得分:1)
您应该在更新它的属性
之前获取Shop
对象
def update
@shop = Shop.find(params[:id])
if @shop.update_attributes(shop_params)
..
答案 1 :(得分:0)
当然,您可以在按钮的onclick操作中指定隐藏的输入值,如下所示:
<%= form_for(@shop, remote: true, data: { confirm: "You sure?"}) do |fa| %>
<%= fa.hidden_field :status, :value => 1 %>
<%= fa.submit "Approve", class: "btn btn-large btn-success", onclick: "shop_status.value = '1'" %>
<%= fa.submit "Reject", class: "btn btn-large btn-danger", onclick: "shop_status.value = '2'" %>
<% end %>