根据动作ID渲染Rails局部 [英] Render a rails partial based on the id of an action
问题描述
我正在建立一个小型电子商务网站,出售各种男女服装.我想根据用户所在的分类法来呈现部分内容.例如,如果用户位于mysite.com/t/women/pant,我想呈现_women.html.erb,或者,如果用户位于在mysite.com/t/men/shirts中,我想渲染_men.html.erb.
I'm building a small ecommerce site that sells a variety of mens and womens clothing. i would like to render a partial based on which taxonomy the user is in. For example, if the user is at mysite.com/t/women/pants I would like to render _women.html.erb, or, if the user is at mysite.com/t/men/shirts I would like to render _men.html.erb.
我有一个分类法模型,该模型有很多类,而分类法模型有很多产品.
I have a Taxonomy model that has_many taxons, and the Taxon model has_many products.
在taxons_controller.rb中,我有:
In taxons_controller.rb I have:
def show
@taxon = Taxon.find_by_permalink(params[:id])
return unless @taxon
@taxonomy = Spree::Taxonomy.all
@taxon_title = Spree::Taxon.all
@searcher = Spree::Config.searcher_class.new(params.merge(:taxon => @taxon.id))
@searcher.current_user = try_spree_current_user
@searcher.current_currency = current_currency
@products = @searcher.retrieve_products
respond_with(@taxon)
end
在分类群中#show我有:(我知道这是错误的)
And in taxons#show I have: (which I know is wrong)
<% @taxon_title.each do |title| %>
<% @taxonomy.each do |taxonomy| %>
<% if title.name == taxonomy.name %>
<%= render "spree/shared/#{title.name.downcase}" %>
<% end %>
<% end %>
<% end %>
当我访问mysite.com/t/women/long-sleeve时,rails调试器将显示:
When I go to mysite.com/t/women/long-sleeve the rails debugger displays :
controller: spree/taxons
action: show
id: women/long-sleeve
我如何访问内部操作的id,以便在控制器/视图中可以执行以下操作:
How do I access the id of the action im inside, so that in the controller/view I can do something like:
'如果id等于'women',则渲染狂欢/共享/#{title.name.downcase}"'
'if id equals 'women' render "spree/shared/#{title.name.downcase}"'
分类名称在哪里?
我想我需要在控制器的show动作中找到(params [:something],但是我对params不太清楚.
I imagine I need to find(params[:something] in the show action of the controller, but I'm a little unclear about params.
* * * @ beck03076这是一个很棒的把戏.非常感谢你.但它仍然无法正常工作.
* * * @beck03076 That's a great trick. Thank you very much. But it's still not working.
在我的控制器中,我输入:
In my controller I put:
@taxon_id = Spree :: Taxon.find(params [:id])
@taxon_id = Spree::Taxon.find(params[:id])
然后在我执行的操作中:
Then in the action I put:
如果@taxon_id == params [:id],则渲染狂欢/共享/妇女"
render 'spree/shared/women' if @taxon_id == params[:id]
当我加载页面时,它说您要查找的页面不存在".我的部分在正确的目录中.我的语法正确吗?
And when I load the page it says 'the page you were looking for doesn't exist'. My partial is in the correct directory. Is my syntax correct?
我的参数是:
{控制器" =>狂欢/类群",动作" =>显示","id" =>妇女/长袖"}
{"controller"=>"spree/taxons", "action"=>"show", "id"=>"women/long-sleeve"}
再次感谢您的帮助!
推荐答案
每当您不清楚参数时,只需将以下行放在操作中并执行操作即可.
Whenever you are unclear about params, just put the lines below in the action and execute the action.
p "****************************"
p params
p "****************************"
现在,转到启动服务器的终端.
Now, goto the terminal in which you started your server.
找到这两个" * ** * ** * ",它们之间的所有内容参数.
Locate those two "*******" and everything thats in between them are params.
params基本上是红宝石哈希.
params is basically a ruby hash.
示例:
参数看起来像这样,{:controller =>"hello",:action =>"bye",:id => 7,:others =>"OK"}
params look like this, {:controller => "hello",:action => "bye", :id => 7, :others => "OK"}
在您的控制器中访问ID,请使用params [:id].(= 7)
In your controller to access the id, use params[:id].(=7)
要访问其他人,请使用params [:others].(="OK")
to access others, use params[:others].(="OK")
这篇关于根据动作ID渲染Rails局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!