Routing nested resources and matching controller - ruby-on-rails

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.

Related

Simple search in the database

I have a problem with a simple browser in my application.
I have already looked for solutions but still displays the following error:
param is missing or the value is empty: expense.
Please help.
Index:
<%= form_tag expenses_path :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" , name: nil %>
Controller:
class ExpensesController < ApplicationController
before_action :set_expense, only: [:show, :edit, :update, :destroy]
# GET /expense expense
# GET /expense.json
def index
#expenses = Expense.search(params[:search])
end
# GET /expense/1
# GET /expense/1.json
def show
end
# GET /expense/new
def new
#expense = Expense.new
end
# GET /expense/1/edit
def edit
end
# POST /expense
# POST /expense.json
def create
#expense = Expense.new(expense_params)
respond_to do |format|
if #expense.save
format.html { redirect_to #expense, notice: 'zostały zapisane.' }
format.json { render :show, status: :created, location: #expense }
else
format.html { render :new }
format.json { render json: #expense.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /expense/1
# PATCH/PUT /expense/1.json
def update
respond_to do |format|
if #expense.update(expense_params)
format.html { redirect_to #expense, notice: 'expense was successfully updated.' }
format.json { render :show, status: :ok, location: #expense }
else
format.html { render :edit }
format.json { render json: #expense.errors, status: :unprocessable_entity }
end
end
end
# DELETE /expense/1
# DELETE /expense/1.json
def destroy
#expense.destroy
respond_to do |format|
format.html { redirect_to #expense, notice: 'Zakupy zostały usunięte.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_expense
#expense = Expense.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def expense_params
params.require(:expense).permit(:date, :price, :category, :where)
end
Expense.rb:
class Expense < ApplicationRecord
def self.search(search)
if search
where (['where LIKE ?',"%#{search}%"])
else
all
end
end
permit the search in your params
def expense_params
params.require(:expense).permit(:date, :price, :category, :where, :search)
end
please correct the form_tag syntax
<%= form_tag(expenses_path,method: :get) do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" , name: nil %>
<% end %>
The error message you report is most likely coming from your expense_params method:
params.require(:expense) means you expect the parameters to be of the form params[:expense][:date], params[:expense][:price], etc.
My best guess is that your form is not actually submitting a GET request and so it's going to the create method rather than the index method.
I think this may be occurring because you are missing a comma between expenses_path and :method => 'get' in your form_tag. It should be:
<%= form_tag expenses_path, :method => 'get' do %>

Rails model form f.select do not assign selected value to model key

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.

Rails How to pass params in cross model system

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

When a Post comment is made, no more can be created, only shown

I am trying to get a post and comment system working, for this however i want only one comment to be made per post. Only as i am trying to create a system as where content will be displayed followed by a comment 7 times in one post... Example...
program model 1 body content 1
Commentmodel1
program model 1 body content 2
Commentmodel2
program model 1 body content 3
Commentmodel3
.etc.etc.
For Me this is the simplest way of being able todo this by creating 7 different comment models, i know there is probably an easier way but as im new this seems the simplest. However i am struggling getting the one comment model to only allow just one comment to be made.
In this application coach is the user.
Here are the files involved, For the Models, program is the basic Post model, and comments is comments.
programs/Show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b><br />
<%= #program.title %>
</p>
<p>
<b>Body:</b><br />
<%= #program.cweekcomments %>
</p>
<%= link_to 'Edit', edit_program_path(#program) %> |
<%= link_to 'Back', programs_path %>
<br /><br /><br />
<i>Comments</i>
<% #program.comments.each do |comment| %>
<p>
<b>Comment:</b>
<% if comment %>
<%= comment.body %>
<br />
<%= link_to 'Edit', edit_program_comment_path(#program, comment) %> | <%= link_to 'Destroy', [#program, comment] , method: :delete, data: { confirm: 'Are you sure?' } %>
<% else %>
<%= form_for([#program, #program.comments.build]) do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>
</p>
<% end %>
Programs_controller.rb
class ProgramsController < ApplicationController
before_filter :authenticate_coach!, :except => [:show]
# GET /programs
# GET /programs.json
def index
#programs = Program.find_all_by_coach_id(current_coach[:id])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #programs }
end
end
# GET /programs/1
# GET /programs/1.json
def show
#program = Program.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #program }
end
end
# GET /programs/new
# GET /programs/new.json
def new
#program = Program.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #program }
end
end
# GET /programs/1/edit
def edit
#program = Program.find(params[:id])
end
# POST /programs
# POST /programs.json
def create
#program = Program.new(params[:program])
respond_to do |format|
if #program.save
format.html { redirect_to #program, notice: 'Program was successfully created.' }
format.json { render json: #program, status: :created, location: #program }
else
format.html { render action: "new" }
format.json { render json: #program.errors, status: :unprocessable_entity }
end
end
end
# PUT /programs/1
# PUT /programs/1.json
def update
#program = Program.find(params[:id])
respond_to do |format|
if #program.update_attributes(params[:program])
format.html { redirect_to #program, notice: 'Program was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #program.errors, status: :unprocessable_entity }
end
end
end
# DELETE /programs/1
# DELETE /programs/1.json
def destroy
#program = Program.find(params[:id])
#program.destroy
respond_to do |format|
format.html { redirect_to programs_url }
format.json { head :no_content }
end
end
end
Comments_controller.rb
class CommentsController < ApplicationController
def new
#comment = #program.comments.build
end
def create
#program = Program.find(params[:program_id])
#comment = #program.comments.create(params[:comment])
redirect_to program_path(#program)
end
def destroy
#program = Program.find(params[:program_id])
#comment = #program.comments.find(params[:id])
#comment.destroy
redirect_to program_path(#program)
end
def edit
#program = Program.find(params[:program_id])
#comment = #program.comments.find(params[:id])
end
def update
#program = Program.find(params[:program_id])
#comment = #program.comments.find(params[:id])
respond_to do |format|
#if #program.comments.update_attributes(params[:comment])
if #comment.update_attributes(params[:comment])
format.html { redirect_to program_path(#program), notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
end
In advance, thanks for your help, much appreciated!
Change your program comment relation to has_one.(has_one :comment in your program.rb)
def create
#program = Program.find(params[:program_id])
if #program.comment
flash[:error] = "Cannot comment more than once"
else
#comment = #program.comments.create(params[:comment])
flash[:notice] = "Comment created"
end
redirect_to program_path(#program)
end

Deep nested resources rails

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)

Resources