我无法更新现有记录,我希望操作不要像编辑操作那样

时间:2018-03-29 17:37:31

标签: ruby-on-rails

有两个问题。

首先,我想启用一个成功更新的操作。 现在我不知道为什么我无法成功更新。

其次, 我想编辑和更新现有记录的列,我发现如果我做一个动作获取并对应一个充当put动作的动作,则get动作被识别为编辑的存在, 我不希望这样,所以我该如何解决?

查看split_now.html.erb

<div class="container">
 <h1>Split</h1>
 <%= form_for @currency, url: { action: 'split', controller: 'currencies'}, method: :put do |f|%>
  <% if @currency.errors.any? %>
      <div id="error_explanation" class="alert alert-danger">
        <h2><%= @currency.errors.count %>error(s) exist(s).</h2>
        <ul>
          <% @currency.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
          <% end %>
        </ul>
      </div>
  <% end %>
  <p>amount whatever you want to issue like 100</p>
  <div class="input-group">
    <%= f.number_field :amount,:class=>'form-control form-group',placeholder:"amount",min:"1",step:"1",required:true%>
  </div>
  <%= f.submit 'done', class: "btn btn-large bg-info" %>
 <% end %>
</div>   

currencies_contoroller.rb

def split_now
  @currency=Currency.find_by_slug(params[:id])
end


def split
   @currency=Currency.find_by_slug(params[:id])
  if CurrencyUser.where(currency_id:@currency.id,owner:true).first.user==current_user
    issue_more(params,@currency)
  else
    redirect_to @currency,alert:"This currency wasn't issued by you"
  end
end

def issue_more(params,currency)
   result=false
   current_amount=currency.amount
  result=currency.increment("amount",params[:amount].to_i)
 currency_user=CurrencyUser.find_by(currency_id:currency.id,owner:true)
 currency_user.increment("amount",params[:amount].to_i)
 return result
end`

路由

 resources :currencies do
  member do
    put :split
    get :split_now
  end
 end
       Started PUT "/currencies/hello/split" for 127.0.0.1 at 2018-03-30 23:50:42 +0900
     Processing by CurrenciesController#split as HTML
   Parameters: {"utf8"=>"✓", "authenticity_token"=>"zZHYVV3AqI6fiqtY47XJTP1iOte1gRzwSPw4NEHHtfWnf0QHNrRnS1fzD+KCIrAm93eKROb1YynGmcwlhx6Vtw==", "currency"=>{"amount"=>"1"}, "commit"=>"done", " 
 id"=>"hello"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
   Currency Load (0.5ms)  SELECT  "currencies".* FROM "currencies" WHERE "currencies"."slug" = $1 LIMIT $2  [["slug", "hello"], ["LIMIT", 1]]
 CurrencyUser Load (0.7ms)  SELECT  "currency_users".* FROM "currency_users" WHERE "currency_users"."currency_id" = $1 AND "currency_users"."owner" = $2 ORDER BY "currency_users"."id" ASC LIMIT $3  
[["currency_id", 2], ["owner", "t"], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CurrencyUser Load (0.3ms)  SELECT  "currency_users".* FROM "currency_users" WHERE "currency_users"."currency_id" = $1 AND "currency_users"."owner" = $2 LIMIT $3  [["currency_id", 2], ["owner", "t"], ["LIMIT", 1]]
  Rendering currencies/split.html.erb within layouts/application
 Rendered currencies/split.html.erb within layouts/application (0.7ms)
 Rendered layouts/_header.html.erb (2.4ms)
 Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 181ms (Views: 145.3ms | ActiveRecord: 2.8ms)

0 个答案:

没有答案