我的表格很简单:

    <%= 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。按“批准”应将其设置为1,将“拒绝”-分别设置为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)

最佳答案

您应该先获取Shop对象,然后再更新其属性

def update
  @shop = Shop.find(params[:id])
  if @shop.update_attributes(shop_params)
    ..

关于ruby-on-rails - Ruby on Rails批准/拒绝按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25259167/

10-12 15:26