heisenberg heisenberg - 5 months ago 26
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
end

class Timeline < ActiveRecord::Base
belongs_to :member
end


While I am inside
http://localhost:3000/timelines/1
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){
$.ajax({
url: '/contests/new',
type: 'GET',
data:
{
"before": type,
"timeline_id": $("#timeline_id").text(),
"video_id": $("#video_id").text()
}
}).success(function(d){
location.replace(d.new_path);
}).fail(function(err){

alert(err.value);
});
}


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])
end

# 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

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.

HTH