heisenberg heisenberg - 1 year ago 83
Ruby Question

Redirect not working in Ruby on Rails

I have two models, one that is called Timelines and other called Contests.

class Contest < ActiveRecord::Base
belongs_to :member
belongs_to :timeline

class Timeline < ActiveRecord::Base
belongs_to :member

While I am inside
this path for instance I have a button that when I click it I want to make a redirection to create a new contest.

<button type="button" onclick="setContest(0)">Before</button>

function setContest(type){
url: '/contests/new',
type: 'GET',
"before": type,
"timeline_id": $("#timeline_id").text(),
"video_id": $("#video_id").text()


But when I click it I got the following error:

ActiveRecord::RecordNotFound in TimelinesController#show
Couldn't find Timeline with 'id'=undefined

# Use callbacks to share common setup or constraints between actions.
def set_timeline
@timeline = Timeline.find(params[:id])

# Never trust parameters from the scary internet, only allow the white list through.

Any idea of what might be going on wrong and what can I do to avoid it?

Answer Source

In your views, add a link:

link_to "New contest", new_contest_path(video_id: params[:video_id], timeline_id: params[:timeline_id])

This will create a link with the ids as the query string.