Rails 3 ruby 1.9
I am trying to pass a "products" id to a "Details" page and get error "Couldn't find Product without an ID"
This is the link in my browser address bar:
http://localhost:3000/performance_details?product_id=8
My Controller
class ProductsController < ApplicationController
def performance_details
#title = "Performance Details"
#products = Product.find(params[:id])
end
The view that's passing the object ID
<%=link_to 'Details', details_path(product_id: product) %>
The view receiving the object ID
<%#products.each do |product| %>
Do some stuff with products.....%>
<%end%>
Routes File
match 'details' => "products#details"
Your parameter name is product_id, not just id. Here is how your controller should look like:
class ProductsController < ApplicationController
def performance_details
#title = "Performance Details"
#products = Product.find(params[:product_id])
end
That's probably how you should do it:
# routes
resources :products do
member do
get :performance_details
end
end
-
# view
# your url will be /products/:id/performance_details
<%=link_to 'Details', performance_details_product_path(product) %>
-
# controller
class ProductsController < ApplicationController
def performance_details
#title = "Performance Details"
#product = Product.find(params[:id])
end
end
Change the #product line in your products controller to the following:
#product = Product.find(params[:product_id])
From your comment below, you're only finding one product (note the change from plural #products to singular #product, so you don't want to use each. Change this:
<%#products.each do |product| %>
Do some stuff with products.....%>
<%end%>
To just do things with #product, rather than product. So get rid of the outer block, and only use #product. I'd overall recommend the refactoring given in Robin's answer.
You don't have id in your params. According to your URL /performance_details?product_id=8, your id is in a variable called product_id.
Related
I'm new to rails and I have this no method error
show.html.erb
<h1>Profile Page for <%= #item.id %></h1>
<p><%= #item.content %></p>
items_controller.rb
class ItemsController < ApplicationController
def new
#item = Item.new
end
def create
item = Item.new(item_Params)
if item.save
redirect_to user_path(item.user)
else
render 'new'
end
end
private
def item_Params
params.require(:item).permit(:user_id, :content)
end
end
I want to create a show page for items/:id and I want to be able to view each item by their id, how should I create it?
this is my show page
Show page for which resource? Is it /users/:id (since you're redirecting to it) or items/:id? Anyway, you need to define #item in show action of corresponding controller. If you add more information about corresponding controller and user-item association, I can help you with it
UPDATE:
Just add to the ItemsController
def show
#item = Item.find(params[:id])
end
Make sure that you have a line resources :items inside routes.rb.
I think you need to read more about rails controllers in guides
BTW: it is a convention in ruby to use snake_case for method naming. Should be 'item_params', not 'item_Params'
You need to add show action which is for show page and You find the item corresponding its Id. so you just write below action in your controller.
def show
#item = Item.find(params[:id])
end
You can find your item in your Item model using find method.
I have a user profile controller called "userinfo" and it's corresponding view. The userinfo index is the root path. In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. It is giving me this error when I click on the image on the view page:
My routes are:
My userinfos_controller:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#userinfors = Userinfo.where(:userinfo_id => #userinformation_user_id)
end
def show
#myvideo = Video.last
end
def new
#userinformation = current_user.userinfos.build
end
def create
#userinformation = current_user.userinfos.build(userinfo_params)
if #userinformation.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
#userinformation.destroy
redirect_to userinfo_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major)
end
def find_userinfo
#userinformation = Userinfo.find(params[:id])
end
end
and my view is:
<%= link_to image_tag("student.png", class: 'right'), userinfo_path(#userinfors) %>
I thought maybe I must include ':index' in the 'before_action :find_userinfo' at the top of my controller. If I do that, the homepage doesn't even load and it gives me this error:
Try below code:
controller
def index
#userinfors = Userinfo.where(:userinfo_id => #userinformation_user_id) #pass id instead of object #userinformation_user_id
end
view
<% #userinfors.each do |u| %>
<%= link_to image_tag("student.png", class: 'right'), userinfo_path(u) %>
<% end %>
Your problem is that you're trying to do perform a lookup based on something that's not an ActiveRecord (database) attribute.
Your root goes to UserinfosController which expects #userinformation_user_id but I can't tell from your code where that comes from.
You need to define your route in order that this will be expecting for an specific param, maybe the user id, and then you're able to add the value within your view, in a link_to helper:
You could modify your routes.rb to expect an id as param:
get '/user_infors/:id', to: 'userinfos#index', as: 'userinfo_path'
Then in your controller, use a find to "find" in the database the user with such id. If you'd like to use where then that would give you a relationship with all the userinfos with the id being passed as param.
If you want so, then use Userinfo.where('userinfo_id = ?', params[:id]):
def index
#userinfors = Userinfo.find(params[:id])
end
And then in your view you can access to #userinfors:
<% #userinfors.each do |user| %>
<%= link_to image_tag 'student.png', class: 'right', userinfo_path(user) %>
<% end %>
I think you could define the index to get all the userinfors and a show method to get an specific one, as you're trying to do.
In my rails app, I have product and order models. Lets say the products controller look like this:
def index
#products = Product.all
#order = Order.new
end
I have listed all products in the index view as divs. I would like to assign a product's id to the order (#order.product_id = product.id) when I click on its div.
= #products.each do |product|
.product
= product.name
= product.price
Any ideas how to do this :? Thank you!
You need to trigger another request that includes the product ID, and land on a new action that assigns the ID to #order.product_id. In this case, it makes no sense to have #order = Order.new in your products#index action; instead you probably want a nested resource for orders within products, and a new controller for handling orders for a product.
config/routes.rb
resources :products do
resources :orders
end
app/controllers/products_controller
def index
#products = Product.all
end
Now, the products#index view can link to the nested route for a new order within a product. This will product a URL like /products/123/orders/new:
app/views/products/index.html.shaml
= #products.each do |product|
.product
= link_to new_product_order_path(product) do
= product.name
= product.price
Finally, you can render the new Order form using the product specified in the route:
app/controllers/orders_controller
def new
#product = Product.find(params[:product_id])
#order = #product.orders.new
end
At the time I have two models: Patient (has many), and Treatment (belongs to).
Until now I displayed the form for a new treatment on the patient show page and all worked fine. But now i want to outsource the treatments form to an new page. To visualize it better:
<%= render "treatments/form" %>
change to:
<% link_to "new", "treatments/form" %>
SO my problem is that I always become an route error:
No route matches [GET] "/patients/treatments/form"
But the routes look so, and i thought they would work:
resources :patients do
resources :treatments
resources :paintings
end
And the controller of the treatments:
class TreatmentsController < ApplicationController
def create
#patient = Patient.find(params[:patient_id])
#treatment = #patient.treatments.create(params[:treatment])
redirect_to patient_path(#patient)
end
def destroy
#patient = Patient.find(params[:patient_id])
#treatment = #patient.treatments.find(params[:id])
#treatment.destroy
redirect_to patient_path(#patient)
end
end
Since your proposed form is really just a way of creating a new patient treatment, you should consider following RESTful conventions and create a new TreatmentsController action called new:
# app/controllers/treatments_controller.rb
class TreatmentsController < ApplicationController
def new
#patient = Patient.find(params[:patient_id])
end
Since your treatments routes are a nested resource of the patients nested resource routes, you'll need to pass the patient_id to your link helper:
<%= link_to "New Patient Treatment", new_patient_treatment_path(#patient) %>
This will enable you to properly access your nested route for treatment#new.
I am new to Rails.I am creating a Rails project in which Product and Character are the models.I have the following Questions.
Q1. Is the given association between the two models is right?
class Product < ActiveRecord::Base
has_many :characters
end
class Character < ActiveRecord::Base
belongs_to :product
end
Q2. I created a link to add new character in the 'show' page of the products such if we click add new character it should display a 'new' page of characters in which i should have a dropdown list of character names(i.e., height,width,weight and color) but it is not.
It is showing NoMethodError in Characters#new error.
Error raised in the below line of my characters/new file.
collection_select(:product, :c_id, #character, :id, :name)
Note: I had created the values for name attribute as height,weight,width,color in the characters before migrating it.
Q3. If that works(i.e., Q2), i want to show the character name and value in the 'show page of products.For this how can i redirect to 'show' page of products..?
My characterscontroller for new,show and create:
def show
#product = Product.find(params[:id])
#character = Character.find(params[:id])
end
def new
#character = Character.new(params[:character])
#product = Product.find(params[:id])
end
def create
#character = Character.new(params[:character])
if #character.save
redirect_to :action => 'show', :id => #product.id
else
render :action => 'new'
end
end
Well now, after entering the values for the character in the characters/new and clicking create button it is trowing the following error
ActiveRecord::RecordNotFound in ProductsController#show
Couldn't find Product without an ID
I want to show the character name and value in the products/show. the above error is stopping me to do that..
My show method in productscontroller:
def show
#product = Product.find(params[:id])
#character = Character.find(:all)
end
The classes are correct to reflect the One-to-Many Relationship.
Problem with NoMethodError appears to be related to how you setup model, routes, controller & view.
Lets say in your model you have
attr_accessible :id, :name (I don't see this in your model and this is why the error!)
In character controller you need something like this:
def new
#character = Character.new
#product = Product.find(params[:character_id])
end
In the view(app/views/products/new.html.erb), you can add the link for new characters as:
<% link_to "Character", new_product_character_path(product) %>
the above assumes, you set your routes correct by having following in your config/routes.rb:
"resources :products"
"resources :characters"