Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a link_to in controller

I need to pass a notice to a view from controller, and I want if can to create some link also to the notice.

My controller:

format.html { redirect_to purchase_order_headers_path, notice: 'PO already has RR with RR ID: ' + rr.rr_id + ', void RR first.' }

Is there any way so I can do it so the [rr.rr_id] will become a link so when the user click on it will go to it's page? Since link_to will return error "undefined method" if put on controller.

Thanks.

like image 812
ksugiarto Avatar asked Feb 21 '13 06:02

ksugiarto


People also ask

How does Link_to work in Rails?

Rails is able to map any object that you pass into link_to by its model. It actually has nothing to do with the view that you use it in. It knows that an instance of the Profile class should map to the /profiles/:id path when generating a URL.

What is a controller Ruby on Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


1 Answers

if you are using rails 3, you can use view_context.link_to(...) in your controller.

UPDATE: with the format.html code

format.html do
  redirect_to purchase_order_headers_path, notice: "PO already has RR with RR ID: #{view_context.link_to(rr.rr_id, receiving_record_header_path(rr.id))} void RR first.".html_safe
end
like image 156
jvnill Avatar answered Sep 21 '22 06:09

jvnill