ruby-on-rails - 保存我的 "Stop"模型实例时未定义的方法

标签 ruby-on-rails ruby ruby-on-rails-4

我的 Rails 应用程序包含旅游路线。每条路线都有许多停靠点和航点,停靠点也有类别。

航点只是路线和停靠点之间的中间模型,因为停靠点可以属于多条路线。

因此,当我尝试保存停止时,出现错误:

undefined method `category' for #<Stop:0x007fa067acea90>

并且“if @stop.save”行以红色突出显示:

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]

在我的停止 Controller 中,我在创建时有这个:

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

在我的航路点 Controller 上,我在创建时有这个:

    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 

结束

这是表单所在的 View :

<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>

有什么想法吗?

谢谢!

最佳答案

您的 Stop 模型中是否有“类别”属性?如果不是,则以下内容没有意义:

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

关于ruby-on-rails - 保存我的 "Stop"模型实例时未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450087/

相关文章:

sql - Btrim 函数在 PostgreSQL 中无法正常工作

css - 如何在 text_field_tag 中添加图标

ruby-on-rails - 数组上的 rails where() sql 查询

ruby - 如何使用 ssl 通过 smtp 使用 ruby​​ 发送邮件(不使用 rails,gmail 没有 TLS)

ruby - 无法执行操作:action=>LogStash::PipelineAction::Create/pipeline_id:main

mysql - 从MySQL到Mongo数据导出: how to preserve creation date?

ruby-on-rails - 覆盖 rails update_all 方法

ruby-on-rails - 无法通过另一个模型访问强参数。 rails 4

ruby-on-rails-4 - 当我的模型包含与同一类型对象的两个关系时,如何构建我的模型?

jquery - Uncaught Rickshaw.Graph 需要对元素index.js.erb 的引用