带有关联的Ruby未定义方法“错误”has_many - >属于

时间:2018-05-02 08:34:06

标签: ruby-on-rails rails-activerecord

我已经检查了200次代码。也许我可以帮你找到一个打破整体的巫师。我创建了一个与酒店相关的房间入口,但也与帐户相关联。我写数据没有任何问题。直到我不通过验证传递数据。当验证也得到满足时,数据会被保存,但是当我不想满足他们的期望时,很自然会出现错误,但很遗憾我找不到错误方法。

Room :: ActiveRecord_Associations_CollectionProxy的未定义方法`errors':0x00007ff8b2a36530

                            <% if @room.errors.full_messages.any? %>
                                <div class="error_explanation">
                                    <h2><%= t('other.errors', :count => @room.errors.full_messages.size) %></h2>
                                    <ul>
                                    <% @room.errors.full_messages.each do |error_message| %>
                                        <li><%= error_message  %></li>
                                    <% end %>
                                    </ul>
                                </div>
                            <% end %>

我的控制器

def new
        @account = Account.find(current_user.account_id)
        @room = @account.rooms.new

        @hotels = @account.hotels.all

        rescue ActiveRecord::RecordNotFound
            flash[:error] = "#{t 'alerts.Not_found'}"
            redirect_to(rooms_manages_path)
    end

    def create
        @account = Account.find(current_user.account_id)
        @room = @account.rooms

        @hotels = @account.hotels.all

        if @room.create(room_params).valid?
          flash[:success] = "#{t 'alerts.Is_save'}"
          redirect_to(rooms_manages_path)
        else
          flash[:warning] = "#{t 'alerts.If_save'}"
          render 'new'
        end

        rescue ActiveRecord::RecordNotFound
            flash[:error] = "#{t 'alerts.Not_found'}"
            redirect_to(rooms_manages_path)
    end

我的模特

class Room < ApplicationRecord

    belongs_to :account
    belongs_to :hotel
    has_many :reservations

    mount_uploaders :images, ImagesroomUploader
    serialize :images, JSON # If you use SQLite, add this line.

    validates :name, presence: true

end

class Account < ApplicationRecord

    has_many :users, inverse_of: :account, dependent: :destroy
    has_many :hotels
    has_many :rooms
    has_many :offers
    has_many :reservations
    has_many :widgets
    has_many :accountsinvoices
    has_many :accountspayments

end

我的观点

<%= form_for(:room, :url => rooms_manages_create_path(), :html => {:id => "form"}) do |f| %>

                                <% if @room.errors.full_messages.any? %>
                                    <div class="error_explanation">
                                        <h2><%= t('other.errors', :count => @room.errors.full_messages.size) %></h2>
                                        <ul>
                                        <% @room.errors.full_messages.each do |error_message| %>
                                            <li><%= error_message  %></li>
                                        <% end %>
                                        </ul>
                                    </div>
                                <% end %>

                                <%= render(:partial => "form", :locals => {:f => f}) %>

                                <div class="text-left">
                                <%= submit_tag("#{t 'other.actions.save'}", class: 'btn btn-primary') %>
                                </div>

                                <% end %>

1 个答案:

答案 0 :(得分:2)

您的create操作将@room设置为集合

@room = @account.rooms

因此您在视图中显示错误消息。相反,尝试

# some code ommitted ...
@room = @account.rooms.create(room_params)
@hotels = @account.hotels.all
if @room.valid?
# some code ommitted ...