<td>
<%=link_to image_tag(phone.image, :class => 'list_image'), :controller => 'phones_feature_controller', :action => 'index', :id => phone.id %>
</td>
I want to have an image link to another controller.
This link is in my index page and its controller is phone. I have another controller phones_feature. I want to have a link to phones_feature's index page
my phones_feature_controller :
def index
#phones=Phone.find(params[:id])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #phones }
end
end
You are adding controller to your controller name.
<%=link_to image_tag(phone.image, :class => 'list_image'),:controller => 'phones_feature', :action => 'index', :id=>phone.id %>
check url_for documentation
Related
I need to add a link to a rss page but i don't know how
def index
#boxes = Box.paginate :page => params[:page], :order => "boxes.id desc", :per_page => 5,
:include => [:suppliers, :manufacturer]
#page_title = 'Catálogo'
respond_to do |format|
format.html
format.xml { render :xml => #boxes }
format.rss { render :layout => false }
end
end
the url to access is http://localhost:3000/catalog.rss but i don't know how to make it like this <%= link_to 'Canal RSS', :action => 'index' %>
In your routes.rb, you add the route first:
get 'catelog' => '<YOUR_CONTROLLER_NAME>#index', :constraints => {:format => :rss}
Then you can use it like this:
<%= link_to 'Canal RSS', catelog_url %>
catelog is the route prefix. You can run rake routes to see it.
I am new to ROR when i edit my location it will gives me following error
No route matches [POST] "/admin/locations/1"
here i am using rails 3.2.12
this is my location controller
class Admin::LocationsController < ApplicationController
def index
#location= Location.order("location desc")
end
def new
#location=Location.new
end
def create
#location = Location.new(params[:location])
if #location.save
# flash[:notice] = 'Location is successfully added in to list.'
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def edit
#location = Location.find(params[:id])
end
def update
#location = Location.find(params[:id])
if #location.update_attributes(params[:location])
#flash[:notice] = 'Category is successfully updated.'
redirect_to :action => 'index'
else
render :action => 'index'
end
end
end
this is my edit.html.erb
<h2>Edit Location</h2>
<%= simple_form_for(:location, :url => {:action => 'update', :id => #location.id}) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
<%= submit_tag("Update",) %> <%= link_to("cancle", {:action => 'index'} )%>
<%end%>
and this is my route.rb
GuestHouse::Application.routes.draw do
devise_for :customers
namespace :admin do
resources :locations
end
and in my index.html.erb as
<%= link_to("Edit", {:action => 'edit', :id => location.id}, :class => 'btn btn-info')%>
<%= simple_form_for(:location,
:url => {:action => 'update', :id => #location.id},
:method => 'put' ) do |f| %>
Pass method in simple_form_for
For an edit form, you likely want to be using the PUT method instead of POST. It looks like you are using [SimpleForm][1], though, which normally would handle constructing the path for a given model for you. Is there any reason you are not passing your Location instance in your call to simple_form_for? I would expect something like the following:
<%= simple_form_for #location do |f| %>
...
Here your routes for admin/location like the following.
admin_locations GET /admin/locations(.:format) admin/locations#index
POST /admin/locations(.:format) admin/locations#create
new_admin_location GET /admin/locations/new(.:format) admin/locations#new
edit_admin_location GET /admin/locations/:id/edit(.:format) admin/locations#edit
admin_location GET /admin/locations/:id(.:format) admin/locations#show
PUT /admin/locations/:id(.:format) admin/locations#update
DELETE /admin/locations/:id(.:format) admin/locations#destroy
so if you want to send the form to 'update' action, you should mention the path like below.
<%= simple_form_for #location, :url => admin_location_path(#location),:html => { :method => "post"} do |f| %>
I am new to rails and trying to limit the number of comments displayed on my Posts Index Page to 2.
Below is my posts_controller:
class PostsController < ApplicationController
before_filter :authorize, :except => [:index, :show]
# GET /posts
# GET /posts.xml
def index
#posts = Post.all(:include => :comments, :order => "created_at DESC")
#comments = Comment.find(:all, :order => "created_at DESC", :limit => 1)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
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.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
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.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
Below is my Post model
class Post < ActiveRecord::Base
has_attached_file :photo, :styles
=> { :medium => "600x600>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:attachment/:id/:style/:filename"
has_many :comments
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
Below is my posts index view
<table>
<tr>
<th>BoxScore</th>
<th>Content</th>
</tr>
</table>
<% #posts.each do |post| %>
<%= image_tag post.photo.url(:medium), :class =>"floatleft" %>
<p>
<%= post.content %>
</p>
<div class="comments center">
<h3>Comments:</h3>
<%= render :partial => post.comments.reverse %>
<div class="links">
<% if admin? %>
<%= link_to "New Post", new_post_path %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
<%= link_to 'Comment on the Game', post %>
</div>
</div>
<% end %>
</div>
Comments Partial Below
<% div_for comment do %>
<p>
<strong>Posted <%= time_ago_in_words(comment.created_at) %> ago
</strong>
by
<br/>
<%= h(comment.commenter) %>
<br/>
<%= h(comment.body) %>
<br/>
<%= link_to 'More Comments', comment.post %>
</p>
<% end %>
I am not getting an error message, I just don't know how to limit the amount of comments we are rendering on Posts index page. Thanks
You can't specify conditions on eager loaded associations, unfortunately. Likewise, you can't limit the number of rows returned based on a condition (to my knowledge, though there are a lot of things I don't know about SQL functions).
So you're stuck with either:
Load all of the comments for the posts you're displaying in a query, and limit the number shown in your application.
Load only 2 comments for each of the posts you're displaying individually.
The optimal solution depends on your use case. If you're expecting to show only 5 posts and have thousands of comments on each, option 1 might not be very performant and option 2 might be a good solution. If you are expecting to show more posts per page and have only a handful of comments on any one (the more likely scenario), the first option is going to be your best bet.
Option 1
# controller
#posts = Post.limit(20).all
#comments = Comment.find(#posts.collect &:id).group_by &:post_id
# view
<% #comments[#post.id].first(2).each do |comment| %>
...
<% end %>
Option 2
# controller
#posts = Post.limit(5).all
# view
<% post.comments.limit(2).each do |comment| %>
I have the following code:
<%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>
'showpdf' is an action in my Letters controller.
My expectation is that this link should yield the following:
http://domain.com/letters/showpdf/id.pdf
But instead, I get:
http://domain.com/letters/showpdf?id.pdf
If the default routes are :controller/:action/:id shouldn't this work?
Do I need to do something in the routes, even though the format for default appear right? Thanks!
Have you tried something like:
<%= link_to "PDF", :action => "show", :id => letter.id, :format => :pdf %>
where your route would be
:controller/:action/:id.:format
and in your controllers "show" action:
respond_to do |format|
format.pdf .....
format.html .....
end
I am building a fairly simple recipe app to learn RoR, and I am attempting to allow a user to save a recipe by clicking a link rather than through a form, so I am connecting the user_recipe controllers 'create' function through a link_to.
Unfortunately, for some reason the link_to is calling the index function rather than the create.
I've written the link_to as
<%= "save this recipe", :action => 'create', :recipe_id => #recipe %>
this link is on the user_recipes/index.html.erb and is calling the 'create' function of the same controller. It doesn't seem to make a difference if I include the :controller or not.
The controllers look like this
def index
#recipe = params[:recipe_id]
#user_recipes = UserRecipes.all # change to find when more than one user in db
respond_to do |format|
format.html #index.html.erb
format.xml { render :xml => #recipes }
end
end
def create
#user_recipe = UserRecipe.new
#user_recipe.recipe_id = params[:recipe_id]
#user_recipe.user_id = current_user
respond_to do |format|
if #menu_recipe.save
format.html { redirect_to(r, :notice => 'Menu was successfully created.') }
format.xml { render :xml => #menu, :status => :created, :location => #menu }
else
format.html { render :action => "new" }
format.xml { render :xml => #menu.errors, :status => :unprocessable_entity }
end
end
In the standard REST scheme the index action and the create action both have the same url (/recipes) and only differ in that index is accessed using GET and create is accessed using POST. So link_to :action => :create will simply generate a link to /recipes which will cause the browser to perform a GET request for /recipes when clicked and thus invoke the index action.
To invoke the create action use link_to {:action => :create}, :method => :post, telling link_to explicitly that you want a post request, or use a form with a submit button rather than a link.
Assuming you have default resources set up in your routes file, i.e. something like this
resources :recipes
The following will generate a link that will create a recipe; i.e. will be routed to the create action.
<%= link_to "Create Recipe", recipes_path, :method => :post %>
For this to work, JS needs to be enabled in your browser.
The following will generate a link that will show all recipes; i.e. will be routed to the index action.
<%= link_to "All Recipes", recipes_path %>
This assumes the default which is a Get HTTP request.