我的Rails应用程序包含旅游路线。每条路线都有许多站点和航点,站点有类别。
航点只是路线和停靠点之间的中间模型,因为停靠点可以属于许多路线。
所以,当我尝试保存止损时,我收到错误:
undefined method `category' for #<Stop:0x007fa067acea90>
&#34; if @ stop.save&#34;行以红色突出显示:
respond_to do |format|
if @stop.save
Waypoint.create(route_id: params[:route_id] , stop_id: @stop.id)
#print out category id or name here
category = params[:category]
在我的停止控制器中我创建了这个:
def create
@stop = Stop.new(stop_params)
respond_to do |format|
if @stop.save
Waypoint.create(route_id: params[:route_id] , stop_id: @stop.id)
#print out category id or name here
category = params[:category]
#once printed save it (stop category)
StopCategory.create(category_id: category, stop_id: @stop.id)
format.html { redirect_to @stop, notice: 'Stop was successfully created.' }
format.json { render :show, status: :created, location:@stop }
else
format.html { render :new }
format.json { render json: @stop.errors, status: :unprocessable_entity }
end
end
在我的航点控制器上我创建了这个:
def create
@waypoint = Waypoint.create(waypoint_params)
redirect_to @waypoint.route
end
这是停止模型:
class Stop < ActiveRecord::Base
validates :description, length: { maximum: 140 }
validates :category, presence: true
#Image Uploader
mount_uploader :stop_image, StopImageUploader
#Relationship with stops and waypoints
has_many :waypoints
has_many :routes, through: :waypoints
# Relationship with categories
has_many :stop_categories
has_many :categories, through: :stop_categories
端
这是表格的视图:
<h2>Create a new stop:</h2><br>
<%= form_for(@stop) do |f| %>
<% if @stop.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@stop.errors.count, "error") %> prohibited this stop from being saved:</h2>
<ul>
<% @stop.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<select name="category" id=" " >
<option value="" disabled selected> Select a Category </option>
<% @categories.each do |category| %>
<option value="<%= category.id %>"> <%= category.name %> </option>
<% end %>
</select>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :stop_image %><br>
<%= f.file_field :stop_image %>
</div>
<div class="field">
<%= f.label :stop_lat %><br>
<%= f.number_field :stop_lat, :class => 'text_field', :step => 'any' %>
</div>
<div class="field">
<%= f.label :stop_long %><br>
<%= f.number_field :stop_long, :class => 'text_field', :step => 'any' %>
</div>
<%= hidden_field_tag :route_id, params[:id] %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h2>Add existing stop</h2>
<br>
<%= form_for(@waypoint) do |f| %>
<div class="field">
<%= f.label :stop_id %><br>
<%= f.number_field :stop_id %>
</div>
<%= f.hidden_field :route_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_route_path(@route) %> |
<%= link_to 'Back', routes_path %>
<br>
<br>
<br>
</div>
有什么想法吗?
谢谢!
答案 0 :(得分:1)
您的Stop模型中是否有'category'属性?如果没有,那么以下内容没有意义:
validates :category, presence: true
这可能是失败的原因。
如果要验证类别的存在,则需要使用以下内容替换该检查:
validate :has_categories
def has_categories # make this private
if categories.blank?
# add errors here
end
end
以下内容也可能有效:
validates :categories, presence: true
答案 1 :(得分:0)
在停止模型中,您应该替换
行validates :category, presence: true
带
validates :categories, presence: true
因为验证器将属性/关联名称作为参数,而不是关联的模型名称。
您的关联称为categories
,因此您需要执行验证。