0
紅寶石版本:爲什麼我的鐵軌動作助手有TypeError?
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
Rails的版本:
Rails 4.1.4
你好,
我做了一個幫手在我的管理指數的觀點我基本管理控制器動作。
application_helper.rb
(...)
def actions_helper(object)
show_link = link_to "<span class=\"glyphicon glyphicon-file\"></span>".html_safe, [:admin, object], :class => 'btn btn-primary', :title => 'Voir '[email protected]_singular_name
edit_link = link_to "<span class=\"glyphicon glyphicon-pencil\"></span>".html_safe, [:edit, :admin, object], :class => 'btn btn-primary', :title => 'Modifier '[email protected]_singular_name
destroy_link = link_to "<span class=\"glyphicon glyphicon-trash\"></span>".html_safe, [:admin, object], :method => :delete, :data => { :confirm => 'Voulez-vous supprimer '[email protected]_singular_name+'?' }, :class => 'btn btn-primary', :title => 'Supprimer '[email protected]_singular_name
"<p class=\"btn-group btn-group-xs\">".html_safe+show_link+edit_link+destroy_link+"</p>".html_safe
end
(...)
它非常適合我的管理頁面控制器,這裏是視圖:
的意見/管理/頁/ index.html.haml
(...)
%td.col-sm-1
= actions_helper(page)
(...)
這裏是我的瀏覽器的結果:
<td class="col-sm-1 tac">
<p class="btn-group btn-group-xs">
<a class="btn btn-primary" href="/admin/pages/linge-de-lit" title="Voir la page">
<span class="glyphicon glyphicon-file"></span>
</a>
<a class="btn btn-primary" href="/admin/pages/linge-de-lit/edit" title="Modifier la page">
<span class="glyphicon glyphicon-pencil"></span>
</a>
<a class="btn btn-primary" data-confirm="Voulez-vous supprimer la page?" data-method="delete" href="/admin/pages/linge-de-lit" rel="nofollow" title="Supprimer la page">
<span class="glyphicon glyphicon-trash"></span>
</a>
</p>
</td>
但是當我嘗試做同樣的事情與我的產品管理器,我有一個類型錯誤,這裏是視圖:
的意見/管理/產品/ index.html.haml
(...)
%td.col-sm-1
= actions_helper(product)
(...)
這裏是我的日誌錯誤:
ActionView::Template::Error (no implicit conversion of nil into String):
58: .badge.background-danger.
59: %span.glyphicon.glyphicon-list-alt
60: %td.col-sm-1.
61: = actions_helper(product)
app/helpers/application_helper.rb:34:in `+'
app/helpers/application_helper.rb:34:in `actions_helper'
app/views/admin/products/index.html.haml:61:in `block in _app_views_admin_products_index_html_haml__1562039986179127831_70358450305840'
app/views/admin/products/index.html.haml:24:in `_app_views_admin_products_index_html_haml__1562039986179127831_70358450305840'
我不認爲這個問題是在我的控制器或我的e因爲當我詢問我的actions_helper只返回#{object}
時,我的管理頁面控制器的結果是#<Page:0x000000035ccaf8>
和我的管理產品控制器#<Product:0x007ffb3618c480>
。
UPDATE
這裏是我的管理產品控制器:
控制器/管理/ products_controller.rb
class Admin::ProductsController < Admin::ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy, :check_favorite, :uncheck_favorite, :check_shop_disponibility, :uncheck_shop_disponibility, :check_web_disponibility, :uncheck_web_disponibility]
def index
@products = Product.all
end
def show
end
def new
@product = Product.new
@pages = Page.all
end
def edit
@pages = Page.all
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to [:admin, @product], notice: 'Product was successfully created.'
else
render action: 'new'
end
end
def update
if @product.update(product_params)
redirect_to [:admin, @product], notice: 'Product was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@product.destroy
redirect_to admin_products_url, notice: 'Product was successfully destroyed.'
end
def check_favorite
@product.update(favorite: true)
redirect_to admin_products_url
end
def uncheck_favorite
@product.update(favorite: false)
redirect_to admin_products_url
end
def check_shop_disponibility
@product.update(shop_disponibility: true)
redirect_to admin_products_url
end
def uncheck_shop_disponibility
@product.update(shop_disponibility: false)
redirect_to admin_products_url
end
def check_web_disponibility
@product.update(web_disponibility: true)
redirect_to admin_products_url
end
def uncheck_web_disponibility
@product.update(web_disponibility: false)
redirect_to admin_products_url
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :description, :brand_id, :price, :minimum_price, :shop_disponibility, :web_disponibility, :purchase_link, :favorite, :page_ids => [], :post_ids => [])
end
end
這裏是我的routes.rb:
Rails.application.routes.draw do
concern :change_priority do
member do
patch :increase_priority
patch :decrease_priority
end
end
concern :change_favorite do
member do
patch :check_favorite
patch :uncheck_favorite
end
end
concern :change_visibility do
member do
patch :check_visibility
patch :uncheck_visibility
end
end
concern :change_disponibility do
member do
patch :check_shop_disponibility
patch :uncheck_shop_disponibility
patch :check_web_disponibility
patch :uncheck_web_disponibility
end
end
namespace :admin do
root 'dashboard#index'
resources :brands
resources :pages, concerns: [:change_priority, :change_favorite, :change_visibility]
resources :posts, concerns: [:change_priority, :change_favorite, :change_visibility]
resources :products, concerns: [:change_favorite, :change_disponibility]
end
root 'welcome#index'
get 'contact' => 'contact#index'
get '/:id', to: 'categories#show', as: 'category'
get '/:category/:id', to: 'pages#show', as: 'page'
end
你可以添加你的routes.rb的相關部分嗎?它可能有幫助。 – 2014-09-26 12:19:27
@MathieuLeTiec粘貼您的產品管理員控制器請 – anusha 2014-09-26 13:29:42
@MathieuLeTiec感謝您的幫助。我更新了我的帖子。 – guduf 2014-09-26 13:47:37