stumped here, probably due to my rails noobness.
I am building a 2 sided market place with bids. using devise for 2 user types relevant here, clients and guides. clients build a trip, guides can view trips and then bid on the trip. i've created a custom landing page for the guides (home) once they sign in which lists client trips, i use a custom view and method in my guides controller to do this, no problem.
however, when from that page i try to go to the next custom page for the guide to view the details of the trip (and then submit a bid), suddenly i get a "Couldn't find Trip with 'id'=" error related to the Trip.find(params[:id]) (if i hardcode Trip.find(1) it works).
guides controller
class GuidesController < ApplicationController
def home
#trips = Trip.all
#guide = current_guide
end
def view_trip
#trip = Trip.find(params[:id])
#guide = current_guide
end
end
guide's home page (which is where the guide clicks to view the trip)
<p>Guides's Email: <%= #guide.email %></p>
<% #trips.each do |trip| %>
<li>Location: <%= trip.location.description %></li>
<li>Details <%= trip.details %></li>
<p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>
<% end %>
and then the error output at the page when i try to visit it/click on the link: amazonaws.com/guides/view_trip.1
Couldn't find Trip with 'id'=
Extracted source (around line #21):
19 def view_trip
20
21 #trip = Trip.find(params[:id])
22 ##trip = Trip.find(1)
23 #guide = current_guide
24 end
Request
Parameters:
{"format"=>"1"}
Change this line:
<p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>
By
<p><%= link_to "View Details and Bid", guides_view_trip_path(id: trip.id) %></p>
Your code should be like that
class GuidesController < ApplicationController
def home
#trips = Trip.all
#guide = current_guide
end
def view_trip
#trip = Trip.find(params[:format])
#guide = current_guide
end
end
I don't know what is your routes.rb design, if you need to take this URL with rails default RESTful URL then your routes.rb looks like this
get 'guides/view_trip/:id(.:format)', to: "guides#view_trip", as: :guides_view_trip
#=> guides_view_trip GET /guides/view_trip/:id(.:format) guides#view_trip
Now you can use the view like below
<%= link_to "View Details and Bid", guides_view_trip_path(trip) %>
#=> /guides/view_trip/1
If you use guides_view_trip_path(id: trip.id) then URL looks like this amazonaws.com/guides/view_trip?id=1 and if you use RESTful which I described then looks like this amazonaws.com/guides/view_trip/1
Or you can use Trip.find(params[:id]) to Trip.find(params[:format])
Hope it helps
Related
I am trying to display only the rows that belong to certain states in my application. I can do it the long way in Javascript, but I would prefer to better understand Rails and queries in the controller. I want to take the users to another page and then show them only that the companies in that state. It would be great to not have to link them to another page. Does anyone know how to do this?
Here is what I have in my controller
def vendors
#vendors = Collective.where(sort: 'Vendor').all
#vendors = #vendors.where(params[:state])
end
My route
get '/vendors/:state', to: 'collectives#vendors'
Then I use the stereotypical method to print a table in a html.erb file.
<% #vendors.each do |company| %>
<tr>
<td><%= company.name %></td>
<td><%= company.state %></td>
etc...
Should your controller code change the where as follows:
def vendors
#vendors = Collective.where(sort: 'Vendor').all
#vendors = #vendors.where(state: params[:state])
end
or better:
def vendors
#vendors = Collective.where(sort: 'Vendor', state: params[:state])
end
Using sessions instead of url params.
This is more or less what you can do, sorry if it is not completly working for your case, just to give an idea.
# view collectives/index (or whatever you have)
<%= form_tag (controller: :collectives, action: :set_status_filter, method: :post) do %>
<%= select_tag(:session_status_filter, options_for_select(#your_list_of_options_for_the_filter)) %>
<%= submit_tag "Set filter" %>
<% end %>
# collectives controller
def index # or whatever, this is the page containing the form and the list to show
#vendors = Collective.where(sort: 'Vendor').all
if session[:session_status_filter] == # etcetera
then #vendors = #vendors.where(state: session[:session_status_filter]) # for example
else # another option just in case, etcetera
end
end
def set_status_filter # this action is called by the form
session[:session_status_filter] = params[:session_status_filter]
respond_to do |format|
format.html { redirect_to *** the view where the form is placed ***, notice: 'the filter is set to: ....' + session[:session_status_filter] } # after the session variable is set the redirects goes to index which uses the session to filter records
end
end
params[:session_status_filter] is passed by the form to collectives#set_status_filter. The value is used to set the session variables. After that the action collectives#set_status_filter redirects to the index, or whatever page you placed the form and the list to show.
My screenshot
I have messages speaker models.
I want to display number of messages each speaker posted on pages index page. When i insert #speaker = Speaker.find(params[:id]) it dispalys error Couldn't find Speaker with 'id'=. I dont understand what am i doing wrong.
my pages controller
class PagesController < ApplicationController
def home
#messages = Message.all.order("created_at DESC")
#speakers = Speaker.all
#speaker = Speaker.find(params[:id])
#count = #speaker.messages.count
#listeners = Listener.all
end
def about
end
def show
#speaker = Speaker.find(params[:id])
end
end
and my home view
<%= #count %> Messages
I think I need to see your routes.rb and the whole home view to better understand. My guess is that you are not actually passing the parameter for it to find.
For example, if your route looks something like this example route:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
When you go to the URL products/1, it knows to pass the parameter id of 1 (params[:id] = 1) to the controller. Otherwise, you need to pass it into the GET request somehow like through a link. Since it is your home page, I doubt that is the intended set up of your app.
My guess is you are attempting to loop through all speakers on the home page. In that case, you can achieve the same effect but with simpler code. This is just a guess, but you may be able to do something like this in your controller and views:
class PagesController < ApplicationController
def home
#speakers = Speaker.all
end
end
<% #speakers.each do |speaker| %>
<%= speaker.name %>: <%= speaker.messages.count %> messages
<ul>
<% speaker.messages.each do |message| %>
<li><%= message.body %></li>
<% end %>
</ul>
<% end %>
This will print out the speaker's name and message count and then all their messages, assuming you set up the relationship between them in your model.
I'm new on rails and I have a book to study them. In one practice, I created a helper in my Application Helper, the test from RSpec work fine, until I have to print the result of my helper. No show any result and no error happens.
application_helper.rb
module ApplicationHelper
def title(*parts)
unless parts.empty?
content_for :title do
(parts << "Ticketee").join(" - ")
end
end
end
end
show.html.erb
<% title(#project.name) %>
projects_controller.rb
class ProjectsController < ApplicationController
def show
#project = Project.find(params[:id])
end
end
and when I go to the show link I supposed to see "Random Project name - Ticketee", however only they show me "Ticketee".
Any help...
<% title(#project.name) %>
Means don't show to the user
<%= title(#project.name) %>
Means show to the user - notice the equals.
I really don't know the way to display error on a view when we post datas which are associated from another model.
My needs :
I have a form displayed in app/view/libraries/show.html.erb
The comments are associated to library in my app. Means that for a library we can have one or many comments.
In model/comment.rb i put that :
validates :name, presence: true
in app/view/libraries/show.html.erb : i need to display error but don't know the way to retrieve it :
<% if #library.comment.errors.any? %>
<%= #library.comment.errors.count %>
<% end %>
CommentsController
class CommentsController < ApplicationController
def new
end
def create
#library= Library.find(params[:library_id])
#comment = #library.comments.create(params[:comment].permit(:name, :commenter))
redirect_to #library
end
end
This does not work someone can please explain to me the way it works ?? I am doing the guide rails getting start but want to add some features simple in it...
Thanks a lot guys !
If all you need is the count, you can put it in flash
def create
#library= Library.find(params[:library_id])
#comment = #library.comments.create(params[:comment].permit(:name, :commenter))
flash[:error_count] = #comment.errors.count
flash.keep[:error_count]
redirect_to #library
end
<% if flash[:error_count] > 0 %>
<%= flash[:error_count] %>
<% end %>
I'm trying to get a link on an articles show page so that when a user clicks write new review it takes them to the link
/comic_reviews/'the article they want to comment on'/reviews/new
where they will be directed to the new reviews page
how can i accomplish this with
In your routes file you would specify a route like this
match '/comic_reviews/:comic_name/reviews/new' => 'reviews#new', via: :get
Then in your reviews controller you would need something like this
reviews_controller.rb
class ReviewsController < ApplicationController
def new
#comic = Commic.find_by_name(params[:comic_name])
if #comic
#review = #comic.reviews.build
render 'new'
else
#Render some error page since comic was not found
end
end
end
You will then have access to #comic and #review in your reviews/new view so you could build a form that just makes a post to create a review and allows you to store it. This should get you going.
Edit
In your new view you'd need to have a form that looks something like this
<%= form_for #review do |f| %>
<%= f.label :some_attribute %>:
<%= f.text_field :some_attribute %><br />
<%= f.submit %>
<% end %>
This will be expecting you have a route to create a review in your routes file and an action in your ReviewsController.
If you are struggling with such topics I suggest you read over this excellent tutorial
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
Or just read through the documentation for Rails API which will give you pretty accurate examples.
You can do this via routes
resources :comic_reviews do
resources :reviews
#probably_some_other_route_here
end
And with restful pattern it will be easy to achieve whatever you want
controller
Someclass < Someotherclass
#some your code
def new
#instance_var = Your_model.new
end
def create
#instance_var = Your_model.new(params[:some_name_here])
if #instance_var.save
redirect_to somewhere
else
render 'new'
end
end
end
Also you'll need form, but i dont think that will cause any troubles