简体   繁体   中英

Rails has_many association issue in controller

I have a company model designed with the devise , and when the company is logged in the company can create the event so that the company has many events and events belong to company the events controller is given as

class EventsController < ApplicationController
  before_action :company_signed_in?
  def index
    @events = Event.all
  end

  def new
    @event = current_company.events.build
  end

  def create
    @event = current_company.events.build(event_params)
    if @event.save
      flash[:success] = "Profile saved"
      redirect_to company_events_path(current_company)
    else
      flash[:error] = "Error"
      render :new
    end
  end

  def show
    @event = current_company.events.where(id: params[:id]).first
  end

  private

  def event_params
    params.require(:event).permit(:name, :company_id, :category_id, :event_date, :event_info, :place, :event_avatar)
  end

end

and the company model has

has_many :events

and the event model has

belongs_to :company

the new view of the event has

   <%= form_for [current_company, @event] do |f| %>
 <%= f.text_field :name %>
    <% end %>

and the show view has

<%= @event.name %>

my routes are

resources :companies do
    resource :company_profile, :events
  end

now what i want to do is the current company can create an event and when the event is created it should be redirected to the show page of the event just produced

i need to create an event so that i can get the url like companies/3/events/3 this type of url

issue is when i am going to the show action i am getting undefined method 'name' Please help ! and in the log i have

Started GET "/companies/3/events" for 127.0.0.1 at 2015-07-30 16:41:54 +0530
Processing by EventsController#show as HTML
  Parameters: {"company_id"=>"3"}
  Company Load (0.3ms)  SELECT  `companies`.* FROM `companies` WHERE `companies`.`id` = 3  ORDER BY `companies`.`id` ASC LIMIT 1
  CompanyProfile Load (0.3ms)  SELECT  `company_profiles`.* FROM `company_profiles` WHERE `company_profiles`.`company_id` = 3 LIMIT 1
  Event Load (0.3ms)  SELECT  `events`.* FROM `events` WHERE `events`.`company_id` = 3 AND `events`.`id` IS NULL  ORDER BY `events`.`id` ASC LIMIT 1
  Rendered events/show.html.erb within layouts/application (12.5ms)
Completed 500 Internal Server Error in 32ms (ActiveRecord: 0.8ms)

ActionView::Template::Error (undefined method `name' for nil:NilClass):
    1: 
    2: <label>Name : </label>
    3: <%= @event.name %>
    4: <%= @event.event_date %></br></br>
    5: <label>Age : </label>
    6: <%= @event.place %></br></br>
  app/views/events/show.html.erb:3:in `_app_views_events_show_html_erb__541678279__634038278'

You should try the below method and see if it works:

def show
@event = Event.find(params[:id])
end

you are not passing id of event to the show action. only company_id is passed as shown in parameters

   Parameters: {"company_id"=>"3"}

so when it queries in the event with id NULL it didnt find any event and hence null.name is crashing

Either pass id of event to the show action. or for testing you should do this

def show
  @event = current_company.events.first
end

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