FriendlyID找不到匹配slug

时间:2015-07-01 10:55:41

标签: ruby-on-rails ruby ruby-on-rails-4 friendly-id

FriendlyID似乎没有找到带有相应slug的数据库条目。

以下是控制台显示的内容:

  

开始GET" /预算/饮料"对于127.0.0.1在2015-07-01 12:42:53   +0200由BudgetsController处理#show as HTML参数:{" id" =>" drink"}预算负载(0.2ms)SELECT"预算"。* FROM   "预算"在哪里"预算"。" slug" ='饮料' ORDER BY"预算"。" id"   ASC LIMIT 1用户负载(0.2ms)SELECT"用户"。* FROM"用户"哪里   #&34;用户"" ID" = 1 ORDER BY"用户"。" id" ASC LIMIT 1 MoneyTransaction   加载(0.5ms)SELECT" money_transactions"。* FROM" money_transactions"   在哪里" money_transactions"。" budget_id" =? [[" budget_id",17]]
  预算负荷(0.1ms)SELECT"预算"。* FROM"预算"哪里   "预算"" USER_ID" =? [[" user_id",1]]已呈现   预算/ _budget_overview.html.erb(5.5ms)已呈现   预算/ _form.html.erb(7.0ms)在内部呈现预算/ show.html.erb   布局/应用程序(26.3ms)在224ms完成200 OK(浏览次数:213.5ms   | ActiveRecord:1.0ms)

     

开始POST" / money_transactions"在2015-07-01获得127.0.0.1   12:43:06 +0200 MoneyTransactionsController处理#create as   HTML参数:{" utf8" =>"✓",   " authenticity_token" = GT;" EPcX4adigoTRY6A1doxWpdMM9eXy / KyqY84zKp855bE =&#34 ;,   " money_transaction" => {" budget_id" =>"饮料","金额" =>" 24&#34 ;,   "标题" =>"焦炭"},"按钮" =>""}用户负载(0.2ms)SELECT&# 34;用户" *。   来自"用户"用户"。" id" = 1 ORDER BY"用户"。" id" ASC限制   1预算负荷(0.2ms)SELECT"预算"。* FROM"预算"哪里   "预算""蛞蝓" ='饮料' ORDER BY"预算"。" id" ASC限制1
  (0.1ms)开始交易预算负荷(0.1ms)SELECT"预算"。*   来自"预算"在哪里"预算"。" id" =?限制1 [[" id",0]]
  (0.1ms)回滚事务(0.0ms)开始事务(0.1ms)   回滚事务重定向到   http://localhost:3000/budgets/drinks完成302发现在14ms   (ActiveRecord:0.7ms)

这表明当我访问页面时,slug存在并且与ID正确相关。然而,在寻找了一个条目"饮料"它只返回ID为0.

  

预算负荷(0.2ms)SELECT"预算"。* FROM"预算"哪里   "预算""蛞蝓" ='饮料' ORDER BY"预算"。" id" ASC限制1
  (0.1ms)开始交易预算负荷(0.1ms)SELECT"预算"。*   来自"预算"在哪里"预算"。" id" =?限制1 [[" id",0]]

money_transaction_controller.rb

def create
    @budget = Budget.friendly.find(params[:money_transaction][:budget_id])
    @money_transaction = MoneyTransaction.new(money_transaction_params)
    @money_transaction.user = current_user
    @money_transaction.date = Time.now.to_date
    @money_transaction.is_positive = 0

    @money_transaction.save

    respond_to do |format|
      if @money_transaction.save
        format.html { redirect_to @budget, notice: 'Transaction added.'}
        format.json { render :show, status: :created, location: @budget }
      else
        format.html { redirect_to @budget, alert: 'Transaction was not valid.'}
        format.json { render json: @money_transaction.errors, status: :unprocessable_entity }
      end
    end
  end

budget.rb

class Budget < ActiveRecord::Base
  belongs_to :user
  has_many :money_transactions

  validates :amount, presence: true
  validates :title, presence: true
  validates :user, presence: true
  validates :amount, numericality: true

  extend FriendlyId
  friendly_id :title, use: :slugged

  def should_generate_new_friendly_id?
    new_record?
  end
end

预算/ show.html.erb

<%= form_for(@budget.money_transactions.build, :html => { :id => 'money_transaction-form' }) do |f| %>
      <%= f.hidden_field :budget_id, :value => params[:id] %>

      <%= f.number_field :amount, id: "transaction-input", placeholder: "#{@budget.title} ", step: 0.01, :autocomplete => :off %>

      <%= f.text_field :title, class: "", placeholder: "purpose", :autocomplete => :off %>

      <%= f.button(type: 'submit', class: "postfix") do %>
          <i class="fa fa-arrow-right"></i>
      <% end %>

<% end %>

的routes.rb

  get '/pages', to: 'pages#index'

  devise_for :users
  resources :users

  resources :budget
  resources :money_transactions


  root :to => 'budgets#index'

如果您需要任何其他文件,请告诉我。我之前使用过friendlyID,从来没有遇到过问题。我希望你能帮助我。

修改 所以我找到了一个解决方法。但我觉得应该有一个更简单的解决方案。我将隐藏的字段更改为:

<%= f.hidden_field :budget_id, :value => Budget.where(:slug => params[:id]).last.id %>

现在它有效。但如果你有更好的解决方案,请告诉我。

1 个答案:

答案 0 :(得分:0)

不要将FriendlyID用于表单参数 - 只需使用直接ID即可。这使rails可以自动添加关联,从而产生更有效的SQL查询。

<%= form_for(@budget.money_transactions.build, :html => { :id => 'money_transaction-form' }) do |f| %>
  <%= f.hidden_field :budget_id, value: @budget.id %>
  <%= f.number_field :amount, id: "transaction-input", placeholder: "#{@budget.title} ", step: 0.01, :autocomplete => :off %>
  <%= f.text_field :title, class: "", placeholder: "purpose", :autocomplete => :off %>
  <%= f.button(type: 'submit', class: "postfix") do %>
    <i class="fa fa-arrow-right"></i>
  <% end %>
<% end %>

这样你可以清理你的控制器:

def create
  @money_transaction = MoneyTransaction.new(money_transaction_params) do |mt|
    mt.user = current_user
    mt.date = Time.now.to_date
    mt.is_positive = 0
  end
  respond_to do |format|
    if @money_transaction.save
      format.html { redirect_to @budget, notice: 'Transaction added.'}
      format.json { render :show, status: :created, location: @budget }
    else
      format.html { redirect_to @budget, alert: 'Transaction was not valid.'}
      format.json { render json: @money_transaction.errors, status: :unprocessable_entity }
    end
  end
end