Jump to content

The ultimate community for Ruby on Rails developers.


Photo

creating a view from a model with multiple associations but limiting to one associated record

rails ruby views controllers routes has_many belongs_to

  • Please log in to reply
No replies to this topic

#1 svajone

svajone

    Passenger

  • Members
  • 3 posts

Posted 20 July 2014 - 12:49 AM

  Restaurant Load (1.5ms)  SELECT * FROM "restaurants" INNER JOIN "restaurant_branches" ON "restaurant_branches"."restaurant_id" = "restaurants"."restaurant_id"
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| resta... | res_... | res_... | crea... | updat... | user_id | resta... | addr... | addr... | addr... | addr... | addr... | addr... | numb... | numb... | email |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
| 27       | DOGG... | WE S... | 2014... | 2014-... | 4       | 28       | 405 ... |         | CHICAGO | IL      | 60666   | USA     |         |         |       |
| 27       | DOGG... | WE S... | 2014... | 2014-... | 4       | 29       | 111 ... |         | CHICAGO | IL      | 60661   | USA     |         |         |       |
+----------+---------+---------+---------+----------+---------+----------+---------+---------+---------+---------+---------+---------+---------+---------+-------+
 
As you can see I have a model restaurants and restaurant_branches. How would I approach if I want to open a restaurant record and ONLY ONE branch? Because right now, my form displays all the branches. How will I open this on my index.html.erb file so that my show.html.erb file will only be one restaurant_branches? Thank you for any help.
 
class RestaurantsController < ApplicationController
  before_filter :authenticate_user!, except: [:index, :show]

  def index
    @restaurant = Restaurant.get_list
  end

  def show
    #@restaurant = Restaurant.select('*').joins(:restaurant_branches).find_by(:restaurant_id => params[:id], :restaurant_branches => {:restaurant_branch_id => params[:id]})
    #@restaurant = Restaurant.joins(:restaurant_branches).find_by(:restaurant_id => params[:id], :restaurant_branches => {:restaurant_branch_id => params[:restaurant_branch_id]})
    @restaurant = Restaurant.find(params[:id])
  end

  def new
    @restaurant = Restaurant.new
  end

  def create
    if user_signed_in?
      @restaurant = Restaurant.new(restaurant_params)
      if @restaurant.save
        redirect_to @restaurant
      else
        render 'new'
      end
    else
      redirect_to new_owner_session_path
    end
  end

  def edit
    @restaurant = Restaurant.find(params[:id])
  end

  def update
    @restaurant = Restaurant.find(params[:id])
    if @restaurant.update_attributes(restaurant_params)
      flash.now[:notice] = "You have successfully updated #{@restaurant.res_name.titleize}."
      redirect_to @restaurant
    else
      render 'edit'
    end
  end

  def destroy
    @restaurant = Restaurant.find(params[:id])
    @restaurant.destroy
    flash.now[:notice] = "#{@restaurant.res_name.titleize} has been deleted!"
    redirect_to restaurants_url
  end

  private

  def restaurant_params
    params.require(:restaurant).permit(:res_name, :res_description, restaurant_branches_attributes: [ :id, :address_line1, :address_line2, :address_line3, :address_line4, :address_line5, :address_line6, :number_phone, :number_fax, :email, :_destroy, pictures_attributes: [ :id, :name, :image, :_destroy] ] ).merge(user_id: current_user.id)
  end
end
<h1>
  <strong>Restaurant Name:</strong>
  <%= @restaurant.res_name %>
</h1>

<p>
  <strong>Restaurant Description:</strong>
  <%= @restaurant.res_description %>
</p>
<ol class="restaurant_branch_fields">
	<% @restaurant.restaurant_branches.each do |f| %>
	  <li>
	    <p><strong>Address</strong><%= f.set_address %></p>
	    <p><strong>Contact Info</strong><%= f.set_contact_info  %></p>
	  </li>
	 <% end %>
</ol>

<p>
<%= link_to "Edit", edit_restaurant_path(@restaurant) %>
<%= link_to "Destroy", @restaurant, :confirm => 'Are you sure?', :method => :delete %>
<%= link_to "View All", restaurants_path %>
</p>

 







Also tagged with one or more of these keywords: rails, ruby, views, controllers, routes, has_many, belongs_to

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users