Ruby on rails 在Rails的视图中显示的类别名称
尝试做:我有一个应用程序,有3个业务类别:业务开发、业务财务和业务管理 我有一个索引视图localhost:3000/categories,它显示指向上述所有业务类别的链接 它们链接到新的操作和新的视图。在该视图中,我有以下表单代码: New.html.erbRuby on rails 在Rails的视图中显示的类别名称,ruby-on-rails,Ruby On Rails,尝试做:我有一个应用程序,有3个业务类别:业务开发、业务财务和业务管理 我有一个索引视图localhost:3000/categories,它显示指向上述所有业务类别的链接 它们链接到新的操作和新的视图。在该视图中,我有以下表单代码: New.html.erb <div align="center"> <h1>What are your important <%= @category.bizdev %> Action items?</h1>
<div align="center">
<h1>What are your important <%= @category.bizdev %> Action items?</h1>
<%= form_for @category do |f| %>
<p>
<p>Store Answer Below:</p>
<%= f.text_field :name, :size => 40, :style => 'height: 40px' %>
</p>
<p>
<%=f.submit 'Save action item' %>
</p>
<% end %> </div>
类别模型
class Category < ActiveRecord::Base
has_many :names
end
这个
名称。@bizdev
显然是错误的。我想你是在尝试下一步:@category.names自己解决-创建,在类别模型中:
def name
"Business Development"
end
然后在视图中调用:@category.name
所以-我必须在我的分类模型中创建一个名为name的方法,它只写“业务开发”。然后,我通过新视图中的:@category.name
来调用它。好的-也就是说,你能更具体地说明要做什么吗?将name
添加到name
类中。我不知道怎么做。我进入rails大约3周了。means=您必须准确地解释要编写的代码。与此同时,我正在尝试很多事情。在Category_params方法中:def Category_params params.require(:Category).permit(:answer,:name)-这仍然会在视图中引发错误:名称的未定义方法我自己解决了它-创建,在类别模型中:def name“Business Development”end,然后在视图中调用:@Category.name下次为任何人回答问题时,明白你必须尽可能具体。人们的大脑不会从模糊的指令中学习。他们按照准确的指示学习。
module CategoriesHelper
def bizdev
bizdev = Name.new
Name.bizdev = "Business Development"
end
end
class Category < ActiveRecord::Base
has_many :names
end
<h1>Select A Business Category To Begin Identifying Action Items</h1>
<ol><li><%= link_to 'Business Admin', 'new' %></li><br><br>
<li><%= link_to 'Business Development/Marketing', 'new' %></li><br><br>
<li><%= link_to 'Financial', 'new' %></li>
</ol>
<%= link_to 'Store random action items', new_facilitate_path %><br><br>
<%= link_to 'See a list of already stored action items', facilitates_path %>
Rails.application.routes.draw do
resources :facilitates
resources :categories
root 'categories#index'
get 'show' => 'facilitates#show'
get 'index' => 'categories#index'
get 'new' => 'categories#new'
get 'bizadmstor' => 'categories#bizadmstor'
get 'bizdevstor' => 'categories#bizdevstor'
get 'bizfinstor' => 'categories#bizfinstor'
get 'bizadmshow' => 'categories#bizadmshow'
get 'categories/show' => 'categories#show'
get 'categories/new' => 'categories#new'
bizdev = Name.new
bizdev.name = "Business Development" # i guess you have name property of this model
def name
"Business Development"
end