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.
Related
<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
I have a file reports/print.html.erb
in reports_controller
def print
#report = Report.find(params[:id])
respond_to do |format|
format.html { render :layout => false }
format.xml { render :xml => #report }
end
end
in routes.rb
match 'reports/print(:id)'
trying to call with
<%= link_to 'Print', report_print_path(:id => #report.id), :method => :put %>
and getting this error:
ActionController::RoutingError in Reports#show
No route matches {:action=>"print", :id=>23, :controller=>"report"}
Where am I going wrong?
Change your route to:
match 'reports/print/:id' => 'controller#print', :via => :put
That may fix it (didn't test the code though, and change the 'controller#print' part to your actual controller name.
made it work with
<%= link_to 'Print', print_url(:id => #report.id) %>
and
match 'print/(:id)' => 'reports#print', :via => :get, :as => :print
No idea why it was giving me problems, there's 4 hours of my life i'll never get back.
Not sure why I'm getting this. I did a bunch of reading and I can't make heads or tails of this.
My controller:
def create
#emails = Email.new(params[:email])
respond_to do |format|
if #emails.save
flash[:notice] = 'Email was successfully created.'
format.html { redirect_to admin_emails_path(:mail_type => #emails.mail_type) }
format.xml { render :xml => #emails, :status => :created, :location => #emails }
else
format.html { render :action => "new" }
format.xml { render :xml => #emails.errors, :status => :unprocessable_entity }
end
end
end
Nothing crazy there. Its a multipart(images) form submission..maybe that has something to do with?
Update
Some irb stuff:
>> admin_emails_path(:mail_type => #emails.mail_type)
"/admin/emails?mail_type=magic_email"
>> admin_emails_path(#emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"
The second example seems to be what it actually is returning, ignoring my additional params in the URL.
I should also note that my edit redirect is identical, and it works perfectly.
Update 2
Just to show how completely helpless this situation is, I've changed my controller to this :
if #emails.save
flash[:notice] = 'Email was successfully created.'
debugger
format.html { render :action => "new" } # <=== WTF ?
format.xml { render :xml => #emails, :status => :created, :location => #emails }
else
And I still get this:
Completed in 7401ms (View: 3, DB: 7) | 406 Not Acceptable [http://localhost/admin/emails.%23%3Cemail:0x109fd2a28%3E]
Routes
admin.resources :emails, :collection => {:test_email => :get}, :member => {:update_current => :get, :send_email => :get, :duplicate => :get} do |email|
email.resources :distributions, :collection => {:delete_dist => :get}
end
Form
- form_for #emails, :url => admin_email_path(#emails), :id => "email_form", :html => {:multipart => true} do |f|
... lots of stuff ..
.clear
%p
= f.submit 'Save Email', :class => "button"
The MIME type for the request is determined by the file extension incoming.
The error here is the following line:
>> admin_emails_path(#emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"
The helper admin_emails_path should not be passed the list of e-mails. This collection path should work on it's own. When you pass in the #emails object, it's trying to encode it into the URL and injecting a period, which rails is parsing like a file extension (the url decoded version of %23%3Cemail:0x109eb6360%3E).
Change the reference from:
admin_emails_path(#emails)
to:
admin_emails_path
...and you will not see these format errors.
In routes.rb I have:
get "survey/show" => "survey#show"
post "survey/step_2" => "survey#step_2"
post "survey/step_3" => "survey#step_3"
And in step_2.html.erb I have:
<%= form_for #result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %>
And in survey_controller.rb I have:
def step_2
#result = Result.new(params[:result])
if #result.save
session[:result_id] = #result.id
render :action => "step_2"
else
render :action => "show"
end
end
def step_3
#result = Result.find(session[:result_id])
if #result.update_attributes(params[:result])
render :action => "step_3"
else
render :action => "step_2"
end
end
And when I submit the form on step_2 I get the following error:
No route matches "/survey/step_3"
I believe Rails form_for method may be making that a PUT request, since the #result object has an id. I believe you should change your form_for line to:
<%= form_for #result,
:url => { :controller => 'survey', :action => 'step_3' },
:html => { :method => :post} do |f| %>
or change the route type to put in routes.rb
You have to use match.
match 'survey/step_3' => 'survey#step_3', :via => 'post'
I might be wrong about the :via, but it's something like that.
So, I got this error
Couldn't find MenuBar with ID=add_page_to_menu
But my code for the link that creates that error is as follows:
<%= link_to "add",
:controller => "admin/menu_bars",
:action => "add_page_to_menu",
:page => page.id,
:menu => #menu_bar.id %>
The URL that I get the error on is
http://example.com/admin/menu_bars/add_page_to_menu?menu=1&page=1
it should look something like example.com/admin/menu_bars/add_page_to_menu/1?menu=1&page=1
(I think, I could be wrong, seeing as how its not working =(
the corresponding action in the controller:
def add_page_to_menu
#menu_bar = MenuBar.find(params[:menu])
#page = LinkPage.find(params[:page])
#menu_bar.link_pages << #page
if #menu_bar.save
format.html { render :action => "edit" }
else
format.html { render :action => "edit" }
format.xml { render :xml => #menu_bar.errors, :status => :unprocessable_entity }
end
end
Routes:
map.namespace "admin" do |admin|
admin.root :controller => :site_prefs, :action => :index
admin.resources :site_prefs
admin.resources :link_pages
admin.resources :menu_bars
end
Your route is going to eval to
http://example.com/admin/menu_bars/:id?menu=1&page=1
so Rails is looking for a MenuBar with an ID of add_page_to_menu. You need to add a member method to the routes for your custom action. The routes should look like this:
map.namespace "admin" do |admin|
admin.root :controller => :site_prefs, :action => :index
admin.resources :site_prefs
admin.resources :link_pages
admin.resources :menu_bars, :member => { :add_page_to_menu => :get }
end
and the link_to should look something like this:
link_to("add", menu_bar_add_page_to_menu_path(#menu_bar, :page => #page.id)
and the url produced should look something like this:
http://example.com/admin/menu_bars/1/add_page_to_menu?page=1
There's still some optimization to be done in that, but I think it will get you beyond this issue at least.