简体   繁体   中英

Rails Association, undefined method

I'm working on two simple model

class Permit < ApplicationRecord
   belongs_to :user, optional: true
   validates :user_id, presence: true
end

class User < ApplicationRecord
  has_many :permits
  has_secure_password
end

In rail console, I am doing the following

user = User.find(10)

Which assign the a User object to user. But when I want to create a permit for the user it gives this error

NoMethodError in PermitsController#create
undefined method `Permit' for #<User:0xaaa3528>

How to solve this problem? Thanks!

This is my permit_controller which the problem I believe is in CREATE action

class PermitsController < ApplicationController
  before_action :set_permit, only: [:show, :destroy]
  def index
    @permits = Permit.all
  end

  def new
    @permits = Permit.new
  end

  def create
    @permits = user.permits.create(permit_params)

    if @permits.save
      redirect_to @permits
      else
        redirect_to contact_path
      end

  end

  def destroy
    Permit.destroy_all(user_id: 1)
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'Permit was successfully canceled.' }
      format.json { head :no_content }
    end
  end

  def show
    @permits = Permit.find(params[:id])
  end

  def update
    respond_to do |format|
      if @permits.update(user_params)
        format.html { redirect_to @user, notice: 'Permit was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_permit
    @permits = Permit.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def permit_params
    params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
  end
end

Permit/new/html.erb

<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permits) do |f| %>

        <%= f.label :"Vehicle" %>
        <%= f.text_field :vehicle_type, class: 'form-control' %>

        <%= f

.label :"License Plate" %> <%= f.text_field :carplate, class: 'form-control' %>

    <%= f.label :"Student ID" %>
    <%= f.text_field :studentid, class: 'form-control' %>

    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>

    <%= f.label :"Department of applicant" %>
    <%= f.text_field :department, class: 'form-control' %>

    <%= f.label :permit_start %>
    <%= f.date_select :permitstart, class: 'form-control' %>

    <%= f.label :permit_end %>
    <%= f.date_select :permitend,  class: 'form-control'  %>


    <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

您必须按以下步骤创建许可证:

user.permits.create(..)

You set before_action :set_permit, only: [:show, :destroy] , that mean you can only use @permits (should be singular) in those 2 methods, I see you use @permits in update. Didn't see you permit user_id for permit_params.

In create @permits = user.permits.create(permit_params) you didn't declare user. You set before_action :set_permit, only: [:show, :destroy] , so why don't @permits.destroy

belongs_to :user, optional: true
validates :user_id, presence: true

optional: true but user_id with presence: true, isn't it conflict? how do you require an optional?

You have more bugs to deal thank you thought.

Good luck

I'm not a rails expert but i'll try to help out with this and try to work out the possible places of errors.

  1. In your schema ensure that in your Permits table you have a user_id column.

  2. Test out that the association works in rails console. user = User.first user.permits If it works, great it means your association is working.

  3. Test your permits create function on its own first without including "User" in it. Add the user aspect in only after your permits is fully functioning!

Hope it helps! Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM