I understand deep nested resource should not be adopted, however i do not know how to convert it properly to a 1 level nested resources. If clean tutorial exists show me the tutorial and I will learn it. If not Then please help with my problem. Here my route.rb
resources :article do
resources :picture
resources :comments do
resources :repcomments
end
end
resources :reports
I can do article, picture, comments but not to sure how to approach the third level of reports. The idea comes that an article can have comments and where the comment could be reported to management. Everythings plays in the article#show pages and here the controller
def show
#art = Article.find(params[:id])
#comments = #art.comments.find(:all, :order => 'created_at DESC')
respond_to do |format|
format.html # show.html.erb
format.json { render json: #art }
end
end
Here the view form in article#show
<%= #art.title %>
<%= #art.content %>
Comment
<%= form_for [#art, current_customer.comments.new] do |f| %>
<%= f.text_area :description %><br />
<%= f.submit "Add Comment" %>
<% end %>
# End of form comments
<% #comments.each do |comment| %>
<%= comment.description %>
<div>Report</div>
<%= form_for [#art, comment, repcomments.new] do |f| %>
<%= f.text_area :body %><br />
<%= f.submit "Report it" %>
<% end %>
<% end %>
I am getting a no define method and not to sure how to approach it. Thanks in advance
Here the route file
article_comment_repcomments GET /articles/:article_id/comments/:comment_id/repcomments(.:format) repcomments#index
POST /articles/:article_id/comments/:comment_id/repcomments(.:format) repcomments#create
new_article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/new(.:format) repcomments#new
edit_article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/:id/edit(.:format) repcomments#edit
article_comment_repcomment GET /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#show
PUT /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#update
DELETE /articles/:article_id/comments/:comment_id/repcomments/:id(.:format) repcomments#destroy
Here the models
class Repcomments < ActiveRecord::Base
belongs_to :customer
belongs_to :article
attr_accessible :acknowledged, :body, :completed, :customer_id, :article_id
end
Here my controller repcomments
class RepcommentsController < ApplicationController
# GET /repcomments
# GET /repcomments.json
def index
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomments = Repcomment.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #repcomments }
end
end
# GET /repcomments/1
# GET /repcomments/1.json
def show
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = Repcomment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #repcomment }
end
end
# GET /repcomments/new
# GET /repcomments/new.json
def new
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = Repcomment.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #repcomment }
end
end
# GET /repcomments/1/edit
def edit
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = #comments.repcomments.find(params[:id])
end
# POST /repcomments
# POST /repcomments.json
def create
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = #comments.repcomments.build(params[:repcomment])
respond_to do |format|
if #repcomment.save
format.html { redirect_to #repcomment, notice: 'Repcomment was successfully created.' }
format.json { render json: #repcomment, status: :created, location: #repcomment }
else
format.html { render action: "new" }
format.json { render json: #repcomment.errors, status: :unprocessable_entity }
end
end
end
# PUT /repcomments/1
# PUT /repcomments/1.json
def update
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = #comment.repcomments.find(params[:id])
respond_to do |format|
if #repcomment.update_attributes(params[:repcomment])
format.html { redirect_to #repcomment, notice: 'Repcomment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #repcomment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /repcomments/1
# DELETE /repcomments/1.json
def destroy
#article = Article.find(params[:article_id])
#comments = #articles.comments.find(params[:comment_id])
#repcomment = #comments.repcomments.find(params[:id])
#repcomment.destroy
respond_to do |format|
format.html { redirect_to repcomments_url }
format.json { head :no_content }
end
end
end
NameError in Article#show
Showing /home/jean/rail/voix/app/views/articles/show.html.erb where line #70 raised:
undefined local variable or method `repcomments' for #<#<Class:0xaf24860>:0xaafe934>
So pretty much is the referencing
Just looking at your code, I can see one problem in your routes: :article and :picture need to be plural.
resources :articles do
resources :pictures
resources :comments do
resources :repcomments
end
end
resources :reports
Also, you don't need to nest :repcomments within :articles. In addition to what you already have in your routes, you can add:
resources :comments do
resources :repcomments #and remove the other "resources :repcomments"
end
Yes, you would have resources :comments defined twice, but that's fine.
If you never do anything with comments outside the article scope, then you can write your routes like that:
resources :comments, only: [] do
resources :repcomments
end
This way Rails wont generate the default rest routes (no /comments, no /comments/:id etc)
Related
sorry for this question but I'm struggling with this issue for hours now and can't find the answer anywhere.
Here is the thing, I have a rails app with "Reservation" and "Space" models with the following relations:
class Reservation < ActiveRecord::Base
belongs_to :space
belongs_to :user
end
class Space < ActiveRecord::Base
belongs_to :condo
has_many :reservations
end
When the user creates a new Reservation, in the form he gets to choose from a dropdown (f.select) the spaces available for him. The f.select in the form look like this:
<div class="field">
<%= #user_spaces = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user_spaces, :id, :name), :prompt => "Select space"
%>
</div>
That select it supose to assign a value to the key "space_id" in the Reservation that is being created (column's table is created). But when I check the last reservation in Rails console, space_id value is "nil". What am I doing wrong?
Thank you very much for your help
Reservation controller file:
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
#reservations = Reservation.all
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
#reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations
# POST /reservations.json
def create
#reservation = Reservation.new(reservation_params)
#user = current_user.id
#reservation.user_id = #user
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :new }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if #reservation.update(reservation_params)
format.html { redirect_to #reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: #reservation }
else
format.html { render :edit }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
#reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
#reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:eventdate)
end
end
Space controller file:
class SpacesController < ApplicationController
before_action :set_space, only: [:show, :edit, :update, :destroy]
# GET /spaces
# GET /spaces.json
def index
#spaces = Space.all
end
# GET /spaces/1
# GET /spaces/1.json
def show
end
# GET /spaces/new
def new
#space = Space.new
end
# GET /spaces/1/edit
def edit
end
# POST /spaces
# POST /spaces.json
def create
#space = Space.new(space_params)
respond_to do |format|
if #space.save
format.html { redirect_to #space, notice: 'Space was successfully created.' }
format.json { render :show, status: :created, location: #space }
else
format.html { render :new }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /spaces/1
# PATCH/PUT /spaces/1.json
def update
respond_to do |format|
if #space.update(space_params)
format.html { redirect_to #space, notice: 'Space was successfully updated.' }
format.json { render :show, status: :ok, location: #space }
else
format.html { render :edit }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# DELETE /spaces/1
# DELETE /spaces/1.json
def destroy
#space.destroy
respond_to do |format|
format.html { redirect_to spaces_url, notice: 'Space was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_space
#space = Space.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def space_params
params.require(:space).permit(:name)
end
end
And full Reservation Form:
<%= form_for(#reservation) do |f| %>
<% if #reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% #reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :eventdate %><br>
<%= f.date_select :eventdate %>
</div>
<div class="field">
<%= #user = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user, :id, :name), :prompt => "Select space"
%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
pretty sure you need to permit the space_id attribute in your strong params.
def reservation_params
params.require(:reservation).permit(:eventdate, :space_id)
end
whats happening is that when you go to create a reservation, youre passing in set of params, that is the output of reservation_params
#reservation = Reservation.new(reservation_params)
if space_id is not being permitted in your strong params, then it will be nil when created.
if this is not the issue, can you post what params are getting to the server, and what the output of reservation_params are.
I am unable to figure out the method to pass params to a link tag. What I want to do is When someone clicks on the Join Group link, the Membership model shall have a new row with group_id as the current group id and the user id as the current user id. The Membership model currently consists of two columns : user_id and group_id that maps users to groups. Can anyone help me with the mistake I am making.
Here is the code
Groups : show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #group.title %>
</p>
<p>
<strong>Desc:</strong>
<%= #group.desc %>
</p>
<p>
<strong>Creator:</strong>
<%= #creator.first_name %>
</p>
<%= link_to 'Join Group', memberships_path(:group_id => #group.id, :user_id => current_user.id ), method: :post %>
<%= link_to 'Edit', edit_group_path(#group) %> |
<%= link_to 'Back', groups_path %>
and here is the Memberships controller
class MembershipsController < ApplicationController
before_action :set_membership, only: [:show, :edit, :update, :destroy]
# GET /memberships
# GET /memberships.json
def index
#memberships = Membership.all
end
# GET /memberships/1
# GET /memberships/1.json
def show
#membership = Membership.find(params[:id])
#user = User.find(#membership.user_id)
#group = Group.find(#membership.group_id)
end
# GET /memberships/new
def new
#membership = Membership.new
end
# GET /memberships/1/edit
def edit
end
# POST /memberships
# POST /memberships.json
def create
#membership = Membership.new(membership_params)
respond_to do |format|
if #membership.save
format.html { redirect_to #membership, notice: 'Membership was successfully created.' }
format.json { render :show, status: :created, location: #membership }
else
format.html { render :new }
format.json { render json: #membership.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /memberships/1
# PATCH/PUT /memberships/1.json
def update
respond_to do |format|
if #membership.update(membership_params)
format.html { redirect_to #membership, notice: 'Membership was successfully updated.' }
format.json { render :show, status: :ok, location: #membership }
else
format.html { render :edit }
format.json { render json: #membership.errors, status: :unprocessable_entity }
end
end
end
# DELETE /memberships/1
# DELETE /memberships/1.json
def destroy
#membership.destroy
respond_to do |format|
format.html { redirect_to memberships_url, notice: 'Membership was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_membership
#membership = Membership.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def membership_params
params.require(:membership).permit(:user_id, :group_id)
end
end
You should have a method in your controller that handles adding a user to a group, something like:
class MembershipsController < ApplicationController
...
def join_group
#user = User.find(params[:user]
#user.group_id = params[:group_id].to_i
end
...
end
Then routes.rb needs a path to that controller method:
get 'memberships/join_group' => 'memberships#join_group'
which will result in a path like memberships_join_group_path that you will use in your link_to:
<%= link_to 'Join Group', memberships_join_group_path(:group_id => #group.id, :user_id => current_user.id ) %>
The path you're currently using is sending params to your index method.
try
def create
#member = current_user.members.build(:group_id => params[:group_id])
if #member.save
flash[:notice] = "You have joined this group."
redirect_to members_path
else
flash[:error] = "Unable to join."
redirect_to members_path
end
end
and use memberships instead of members
I'm fairly new to rails, building my first app. I'm running rails 4 w/ bootstrap 3. I'm trying to get a complex form to work. I have two models:
class Employee < ActiveRecord::Base
belongs_to :company
belongs_to :user, :through => :company
has_one :position
accepts_nested_attributes_for :position
end
class Position < ActiveRecord::Base
belongs_to :employees
accepts_nested_attributes_for :employees
end
I have a form where the User can create a new job title (Position Model) and select the employees (Employees Model) that position will be applied to. Basically it's a single form that will add fields to 2 different database tables (Position and Employee).
This is my view:
<%= simple_form_for(#position) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :job_title %>
<%= f.input :job_description %>
</div>
<%= f.fields_for :Employee do |f| %>
<%= f.input :employee_title, label: "Apply to:", collection: Employee.all, label_method: :first_name, as: :check_boxes %>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Below are the controllers:
class PositionsController < ApplicationController
before_action :set_position, only: [:show, :edit, :update, :destroy]
# GET /positions
# GET /positions.json
def index
#positions = Position.all
end
# GET /positions/1
# GET /positions/1.json
def show
end
# GET /positions/new
def new
#position = Position.new
end
# GET /positions/1/edit
def edit
end
# POST /positions
# POST /positions.json
def create
#position = Position.new(position_params)
respond_to do |format|
if #position.save
format.html { redirect_to #position, notice: 'position was successfully created.' }
format.json { render action: 'show', status: :created, location: #position }
else
format.html { render action: 'new' }
format.json { render json: #position.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /positions/1
# PATCH/PUT /positions/1.json
def update
respond_to do |format|
if #position.update(position_params)
format.html { redirect_to #position, notice: 'position was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #position.errors, status: :unprocessable_entity }
end
end
end
# DELETE /positions/1
# DELETE /positions/1.json
def destroy
#position.destroy
respond_to do |format|
format.html { redirect_to positions_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_position
#position = Position.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def position_params
params.require(:position).permit(:position_title, :position_description, :position_create_date)
end
end
class EmployeesController < ApplicationController
# encoding: UTF-8
before_action :set_employee, only: [:show, :edit, :update, :destroy]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to #employee, notice: 'Employee was successfully created.' }
format.json { render action: 'show', status: :created, location: #employee }
else
format.html { render action: 'new' }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to #employee, notice: 'Employee was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:first_name, :last_name, :employee_title)
end
end
The problem I'm facing is I get the form to render perfectly, but when I submit it, only fields that belong to the Position model get recorded. The :employee_title stays blank. Any suggestions what the problem is?
Thank you!!
Too many points to fit in a comment:
belongs_to :employees should be singular: belongs_to :employee
Your fields_for should be like (differentiate the ff from parent form):
<%= f.fields_for :Employee do |ff| %>
<%= ff.input :employee_title, label: "Apply to:", collection: Employee.all,
label_method: :first_name, as: :check_boxes %>
<% end %>
If it doesn't work, supply your controller also and I'll update my answer.
Edit:
After seeing your controllers, it seems most likely to be case of unpermitted params.
In position_controller.rb add employee params to position params
def position_params
params.require(:position).permit(:position_title, :position_description, :position_create_date, employees_attributes: [:first_name, :last_name, :employee_title])
end
I've had a problem over the last few days getting my nested resources to create and display properly. There are tons of similar questions on StackOverflow and lots of blog posts on this, but they all seem to either deal with a older version of Rails or a different issue. I'm at the point where once I finally fix something, another error pops up. I've narrowed it down to me making a stupid mistake or typo being too inexperienced to notice.
I have a Jobs model that belongs to a Venue model. The venue works fine and I've even gotten as far as to be able to go to my nested Jobs index under each Venue and bring up the New and Edit forms, but going to 'Show' or creating a new Job caused an undefined method error. After a lot of searching I found plenty with the same problem and tried to implement their fixes, but now I'm getting a Routing Error.
Most of my confusing comes from when to leave off the #, when to use :venue_id instead of :id in params, etc. Every example I see seems to have a different way and I can't seem to make any of them work for me.
Any bump in the right direction would be extremely helpful.
The Routing Error
No route matches {:action=>"show", :controller=>"jobs", :venue_id=>#<Venue id: 1, name: "Burger Chef", city: "Chicago", state: "Illinois", areacode: 60614, created_at: "2013-02-05 21:33:41", updated_at: "2013-02-06 23:01:05", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>}
routes.rb
Twist::Application.routes.draw do
resources :users
devise_for :users
resources :venues do
resources :jobs
end
end
jobs_controller.rb
class JobsController < ApplicationController
# GET /jobs
# GET /jobs.json
before_filter :get_venue
def get_venue
#venue = Venue.find(params[:venue_id])
end
def index
#jobs = #venue.jobs
respond_to do |format|
format.html # index.html.erb
format.json { render json: #jobs }
end
end
# GET /jobs/1
# GET /jobs/1.json
def show
#job = #venue.job.find(params[:id])
if params[:id]
#venue = Venue.where(:id => params[:id]).first
#jobs = #venue.job_url
else
#jobs = Jobs.all
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: #job }
end
end
# GET /jobs/new
# GET /jobs/new.json
def new
#job = #venue.jobs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #job }
end
end
# GET /jobs/1/edit
def edit
#job = #venue.job.find(params[:venue_id])
end
# POST /jobs
# POST /jobs.json
def create
#job = #venue.jobs.new(params[:job])
respond_to do |format|
if #job.save
format.html { redirect_to :action => :show, :id => #venue.id,
notice: 'Job was successfully created.' }
format.json { render json: [#venue, #job],
status: :created,
location: [#venue, #job] }
else
format.html { render action: "new" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# PUT /jobs/1
# PUT /jobs/1.json
def update
#job = Job.find(params[:id])
respond_to do |format|
if #job.update_attributes(params[:job])
format.html { redirect_to #job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
#job = Job.find(params[:id])
#job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
end
venues_controller.rb
class VenuesController < ApplicationController
# GET /venues
# GET /venues.json
def index
#venues = Venue.all
if params[:name]
#user = User.where(:name => params[:name]).first
#venues = #user.venues
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #venues }
end
end
# GET /venues/1
# GET /venues/1.json
def show
#venue = Venue.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #venue }
end
end
# GET /venues/new
# GET /venues/new.json
def new
#venue = Venue.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #venue }
end
end
# GET /venues/1/edit
def edit
#venue = Venue.find(params[:id])
#if session[:user_id] != #venue.user_id
# flash[:notice] = "Sorry, you cannot edit this venue."
# redirect_to(venues_path)
# =>end
end
# POST /venues
# POST /venues.json
def create
#venue = Venue.new(params[:venue_id])
respond_to do |format|
if #venue.save
format.html { redirect_to #venue, notice: 'Venue was successfully created.' }
format.json { render json: #venue, status: :created, location: #venue }
else
format.html { render action: "new" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# PUT /venues/1
# PUT /venues/1.json
def update
#venue = Venue.find(params[:id])
respond_to do |format|
if #venue.update_attributes(params[:venue])
format.html { redirect_to #venue, notice: 'Venue was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# DELETE /venues/1
# DELETE /venues/1.json
def destroy
#venue = Venue.find(params[:id])
#venue.destroy
respond_to do |format|
format.html { redirect_to venues_url }
format.json { head :no_content }
end
end
end
job.rb
class Job < ActiveRecord::Base
attr_accessible :description, :name, :requirement, :venue_id
validates :name, :presence => true, :length => { :minimum => 3 }
belongs_to :venue
end
venue.rb
class Venue < ActiveRecord::Base
attr_accessible :areacode, :avatar, :city, :name, :state
has_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
/jobs/show.html.erb
<p id="notice"><%= notice %></p>
<% #job = Job.find(param[:venue_id]) %>
<p>
<b>Name:</b>
<%= #job.name %>
</p>
<p>
<b>Company:</b>
<p><%= #venue.name %></p>
<p><%= link_to #job.venue.name, venue_path(#venue) %></p>
<p>
<b>Job:</b>
<%= #job.job_id %>
</p>
<p>
<b>Description:</b>
<%= #job.description %>
</p>
<p>
<b>Requirement:</b>
<%= #job.requirement %>
</p>
<%= link_to 'Edit', edit_venue_job_path(#venue, #job) %> |
<%= link_to 'Back', venue_jobs_path(#venue, #job) %>
**/jobs/index.html.erb**
<div class="usergrid">
<% jobs = #venue.jobs %>
<% #venue.jobs.each do |job| %>
<div class = "user venue">
<p>
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
<%= link_to 'Edit', edit_venue_job_path(#venue, job) %><br/>
<%= link_to 'Delete', venue_jobs_path(#venue, #job), :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
<div style="clear:both"></div>
</div>
<%= link_to 'New Job', new_venue_job_path(#venue) %>
I realize that...
This might be a really obvious fix, but being new to Rails there are definitely still some fundamentals that I don't completely understand.
If I posted too much or too little code please let me know, I'm not entirely sure how much is the least necessary for this.
There might be more than one error, there might be a lot of errors, and many of them could have been caused by me trying to fix this when I really didn't know what I was doing. I wanted to fix this myself, but I can't handle messing with it anymore when I'm really just making it worse.
I've tried messing with the routes, changing the actual links and routes, messing with scope, and as many of the common fixes for these errors that I could find and none of them seemed to help.
Thanks!
The error is saying that there is no route for the given params :
{:action=>"show", :controller=>"jobs", :venue_id=> "an_id"}
You can check that by running rake routes, and you'll see that, as jobs is nested under venue, the jobs#show controller actions needs two parameters : the venue_id (which is the job's 'parent') and the id which is the job id.
I quickly checked your code, and I think that one one of the things that causes the error is this line :
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
this should be
<h2><%= link_to job.name, venue_job_path(#venue, #job) %></h2>
Basically you'll get that kind of error, whenever you'll try to render a link to a job without providing the venue.
Let me know if you need more details or more information.
I am trying a an simple association which should work. I followed the tutorial at http://ruby.railstutorial.org/chapters/ and at chapter 10
Here what I have wrote down
model/customer
class Customer < ActiveRecord::Base
has_many :posts, dependent: :destroy
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
model/post
class Post < ActiveRecord::Base
belongs_to :customer
attr_accessible :description, :post_finish_at, :post_how, :post_location
controller/customers
class CustomersController < ApplicationController
before_filter :signed_in_customer, only: [:edit, :update]
before_filter :correct_customer, only: [:edit, :update]
def index
#customers = Customer.all
end
def show
#customer = Customer.find(params[:id])
#posts = #customer.posts
end ...
controller/post -- should be irrelevant since i am doing a partial
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render json: #post, status: :created, location: #post }
else
format.html { render action: "new" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
view/customer/show.html.erb
<div class="posts">
<%= render 'posts/post'%>
</div>
view/post/_post.html.erb
<li>
<%= #posts.each do |post| %>
<%= post.title %>
<% end %>
</li>
Here what the output look like
[]
Why?
Thanks
It looks to me like you've got a couple things wrong. First, the partial (view/post/_post.html.erb) should be rendering a single post, not the whole collection of them. Second problem is that you're not passing anything to the partial.
There's a handy shorthand for rendering collections, where if you just render the collection, it will automatically look for a partial with the same name and render it for each model in the collection.
See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
So I think this should do what you want (I added <ul></ul> tags to make it an unordered list):
view/customer/show.html.erb
<div class="posts">
<ul>
<%= render #posts %>
</ul>
</div>
view/post/_post.html.erb
<li>
<%= post.title %>
</li>
It looks like customer doesn't have a posts association, but only an events association. I'd start there