Orz
Orz

Reputation: 155

How to pass parameter to another controller and open new page in ruby

In my project info page, I need to generate a report upon user selected project number and date. How can I open a report page(say from noisedevice/report.html.erb) from project info view( project/report.html.erb)?

I feel need to pass parameter from view page to another controller(noisedevice's controller)then call some function(or router??) to generate a new report page for noisedevices's report. How can it be done?what are the knowledge I need to know before process?

Thank you.

Upvotes: 0

Views: 1148

Answers (2)

Prakash Murthy
Prakash Murthy

Reputation: 13057

From project/report.html.erb, the noisedevice/_report.html.erb view can be called with the render method as follows:

<%= render "noisedevice/report", locals: { some_date: some_date, project_id: project_id } %>

See 2.2.3 Rendering an Action's Template from Another Controller at RailsGuides for more detail about calling view of another controller.

And 3.4.4 Passing Local Variables for details about passing local parameters.

The entire page on that guide is worth going over.

Upvotes: 1

miler350
miler350

Reputation: 1421

You can pass parameters via links like this:

<%= link_to new_noisedevice_path({ report_id: @report.id, date: @date })

This will generate a get request with the parameters appended to the URL like this:

http://localhost:3000/noisedevices/new?report_id=5&date=xxx

You can then grab the parameter like this:

@report = Report.find(params[:report_id])
@date = params[:date]

Upvotes: 2

Related Questions