Link_to Routing Issue With Nested Resources - ruby-on-rails

I have two models Jobs and Questions. A job has many questions and questions belong to a job.
I've set up the resources in the model, as well as the routes. I am having an issue trying to link_to the Show method of the questions controller on the questions#index page. My rake routes say that the path should be job_question_path with the two necessary :id's being :job_id and :id , so I tried:
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
and got the error:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil, :id=>#<Question id: 1, job_id: 1, question1: "sfsdfssfs", question2: "sfsdfs", question3: "sfsdf", question4: "sfsdfsf", question5: "sfsfsfs", created_at: "2011-06-21 03:25:12", updated_at: "2011-06-21 03:25:12">}
I've tried multiple other combos and nothing is seeming to work, I keep getting:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil }
or some combination of that.
The part I don't get is that I can put in the url /jobs/1/questions/1 and it takes me to the show page, so I am assuming that my questions#show methods are ok. See below for the rest of my code.
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
</tr>
<% end %>
Questions Controller
def index
#questions = Question.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #questions }
end
end
def show
#job = Job.find(params[:job_id])
#question = #job.questions.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #question }
end
end
Models
class Job < ActiveRecord::Base
has_many :questions
class Question < ActiveRecord::Base
belongs_to :job
Routes.rb
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
See this https://gist.github.com/1032734 for my rake routes.
Thanks for any help in advance, i've been at this for a while now and just can't figure out the solution. Please let me know if you need any more info.

may be so?
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<%= link_to 'Show', job_question_path(question.job_id, question.id) %>
</tr>
It have to work. Or haven't you 'job_id' field in Questions table ?

Related

ActionController::UrlGenerationError in Interfaces#index

I am trying to delete my interface,and it said
No route matches {:action=>"show", :controller=>"interfaces", :project_id=>#}, missing required keys: [:id].
Because I do not know which arguments should be put into <%=link_to '删除', project_interface_path()%>
I have tried many different arguments into a path.
interface_controller.rb
def destroy
#interface = #project.interfaces.find(params[:project_id])
redirect_to project_interfaces_path if #interface.destroy
end
def index
#interfaces = Interface.all
#project = Project.find(params[:project_id])
end
def new
#project = Project.find(params[:project_id])
#interface = Interface.new
end
def create
#project = Project.find(params[:project_id])
#interface = #project.interfaces.new(interface_params)
if #interface.save
redirect_to project_interfaces_path
else
render :new
end
end
interface/index.html.erb
<% #interfaces.each do |interface| %>
<tr>
<th scope="row">1</th>
<td><%=interface.name %></td>
<td><%=interface.desp %></td>
<td><%=interface.method_type_id %></td>
<td><%=interface.request_url %></td>
<td><%=interface.request_eg %></td>
<td><%=interface.response_eg %></td>
</tr>
<td><%= link_to '删除', project_interface_path(interface),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>
This is my route file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "method_types#index"
resources :method_types
resources :field_types
resources :projects do
resources :interfaces
end
end
Try this:
<%= link_to '删除', project_interface_path(#project.id, interface.id),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>

Rails - Updating records using Best in Place

I'm building a marketplace app where I'm trying to use the Best in Place gem to allow sellers to add a tracking number for each of their orders.
I get a NoMethodError which I'm not able to resolve.
NoMethodError in Orders#sales
undefined method `tracking' for nil:NilClass
The error points to the best in place line below in the view page. This view page is based on the method Sales (in the controller below) where I filter for orders for that particular seller.
Here is my routes.rb with order routing. Since orders need not be edited or destroyed, I didn't create an edit or delete route.
resources :listings do
resources :orders, only: [:new, :create, :update]
collection { post :import }
end
Here is a snippet from my orders controller
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :check_user, only: [:edit, :update]
def sales
#orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def update
#order.update_attributes(params[:order])
end
def check_user
if current_user.id != #seller && current_user.name != "admin admin"
redirect_to root_url, alert: "Sorry, you are not the seller of this listing"
end
end
Here is my view page:
<table class="table table-striped table-bordered">
<tr>
<th class="col-md-2">Image</th>
<th class="col-md-2">Item</th>
<th class="col-md-1">Price</th>
<th class="col-md-2">Customer</th>
<th class="col-md-2">Date Sold</th>
<th class="col-md-2">Shipment Tracking #</th>
<th class="col-md-1">Carrier (UPS, USPS, etc.)</th>
</tr>
<% #orders.each do |order| %>
<tr>
<td><%= image_tag order.listing.image.url(:thumb) %></td>
<td><%= order.listing.name %></td>
<td><%= number_to_currency(order.listing.price) %></td>
<td><%= order.buyer.name %></td>
<td><%= order.created_at.strftime("%B %-d, %Y") %></td>
<td><%= best_in_place #order, :tracking, :type => :input %> </td>
<td><%= best_in_place #order, :carrier, :type => :input %></td>
</tr>
<% end %>
</table>
Been stuck on this for a while. Appreciate any help on this.
I think the problem is that you are calling #order inside your .each method.
Try:
<%= best in place order, :tracking, :type => :input %>
You will need to change the next line in your view as well.
I figured it out. The problem was that since I was using best_in_place in a non-Activerecord environment (as part of a table with a list of orders), I needed to pass the order id explicitly. I found this in the best_in_place documentation https://github.com/bernat/best_in_place#non-active-record-environments
I created a custom route for the update action
put 'orderupdate' => "orders#update"
Then in my do loop in the view, I used the custom path for the route above and passed the order id to that route.
<% #orders.each do |order| %>
<tr>
<td><%= order.id %></td>
<td><%= image_tag order.listing.image.url(:thumb) %></td>
<td><%= order.listing.name %></td>
<td><%= number_to_currency(order.listing.price) %></td>
<td><%= order.buyer.name %></td>
<td><%= order.created_at.strftime("%B %-d, %Y") %></td>
<td><%= best_in_place order, :tracking, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
<td><%= best_in_place order, :carrier, :type => :input, :url => orderupdate_path(id: order.id) %> </td>
</tr>
<% end %>
Here is the update method in my controller:
def update
#order = Order.find(params[:id])
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to sales_url, notice: 'Order updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
Hope this helps someone!

Rails kaminari pagination for child table

I have 2 classes: Posts and Comments, where posts has_many :comments and comments belongs_to post.
Each of my posts has a show page with a list of comments and I would like to paginate the comments. With the current code I have, I'm showing a list of all the comments on all the pages. So, If i have 10 comments and I want to have 2 on each page, I get 5 pages with the original 10 comments on it. Could someone shed some light?
My Code:
Posts controller:
def show
#post = Post.find(params[:id])
#comments = #post.comments.page(params[:page]).per(3)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
"Show" views:
<%= paginate #comments %>
<% #post.comments.each_with_index do |comments, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= comment.date %></td>
<td><%= comment.text %></td>
</tr>
<% end %>
</table>
You need to use the paginated object in the view, not get them fresh from the database:
<% #comments.each_with_index do |comments, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= comment.date %></td>
<td><%= comment.text %></td>
</tr>
<% end %>
This gets them fresh, unpaginated:
#post.comments

rails unit/test get ActionView::Template::Error: undefined method `name' for nil:NilClass

there is a easy dependencies in my exercise_project
patients belongs_to location
location has_many patients
my patients_controller.rb file
class PatientsController < ApplicationController
def location
#location = Location.find(params[:id])
place = #location.id
#patients = Patient.where(:location_id => place)
end
def index
#patients = Patient.paginate(:page => params[:page], :per_page => 20).order('id DESC')
respond_to do |format|
format.html
format.json { render json: #patients }
end
end
others are same with which generate by rails default
my patients#index file
<table>
<% #patients.each do |patient| %>
<tr>
<td><%= patient.name %> </td>
<td><%= patient.birthday %> </td>
<td><%= patient.gender %></td>
<td><%= medical_record(patient.id) %></td>
<td><%= patient.status %></td>
<td><%= link_to(patient.location.name, location_path(patient.location)) %> </td>
<td><%= patient.viewed_count %></td>
</tr>
<td><%= link_to 'Show', patient_path(patient) %></td>
<td><%= link_to 'Edit', edit_patient_path(patient) %></td>
<td><%= link_to 'Destroy', patient_path(patient), :method => :delete, :confirm => 'Are YOU SURE?' %></td>
<% end %>
</table>
<%= link_to "create patient", new_patient_path %><hr>
<%= link_to "Locations", locations_path %>
and I write a unit/test for the controller
require 'test_helper'
class PatientsControllerTest < ActionController::TestCase
def setup
#patient = patients(:one)
#location = locations(:one)
#patient.location_id = #location.id
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:patients)
end
end
When I run the test, it returns
1) Error:
test_should_get_index(PatientsControllerTest):
ActionView::Template::Error: undefined method `name' for nil:NilClass
I do know it must be the location method in the controller that makes it, but I can't find a way to solve this.
How could write the right test for this controller?
---------------------updated------------------------------------
I change it to this
require 'test_helper'
class PatientsControllerTest < ActionController::TestCase
def setup
#patient = patients(:one)
#location = locations(:one)
#patient.location_id = #location.id
end
test "should get index" do
get :location
get :index
assert_response :success
assert_not_nil assigns(:patients)
end
end
and get ActionController::RoutingError: No route matches {:controller=>"patients", :action=>"location"}
my routes.rb
Patients::Application.routes.draw do
resources :locations
resources :patients
root to: "locations#index"
match "patients/location/:id", :to => "patients#location", :as => :location_patients
end
get :location
...(and more text because stackoverflow requires 30 character answers.)

Displaying Nested Scaffolds on Index

I'm trying to display something like this on posts/index.html.erb
Post #1
Comment #1 for Post #1
Comment #2
Post #2
Comment #1 for Post #2
etc.
It works fine if I go to /posts/1/comments/, /posts/2/comments/ etc
Since it's using the index file, there is no :post_id in the URL and it throws a nil error. The models use the appropriate have_many and belongs_to.
Here's part of routes.rb
resources :posts do
resources :comments
end
resources :posts
Here's part of my posts_controller.rb
def index
#posts = Post.all
#comments = params[:post_id][:desc]
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
end
end
Here's part of index.html.erb
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<tr><td><%= #comments %></td></tr>
<% end %>
</table>
Thanks!
Well, since comments belongs_to a given post, you just need to have a separate loop in the view to iterate over each of the comments for a given post.
So take the #comments var out of your controller and index view, and do this in the index view instead where you currently have #comments:
<% for comment in post.comments %>
<tr><td><%= comment.user_name %><%= comment.text %></td></tr>
<% end %>
I made the user_name and text attrs up of course you would use whatever is in your comment model.
ian.

Resources