0

I am following the example of SO # 9945620, Making a table row into a link in Rails The params being sent to the controller are {"controller"=>"cpe_events", "action"=>"cpe_course_description_live", "format"=>"15"} I assume the format is automatically being generated by rails. I don't, however, understand why it's adopting the row / record's id and why the cpe_events params or id aren't being passed.

EDIT: I should probably note that the table is rendered using Bootstrap, class="span12 table table-hover table-striped table-bordered", since it's also written in jQuery.

Full code below the following notes.

Here's the data-link that's throwing the error: <tr data-link="<%= cpe_course_description_live_path(cpe_event) %>" > That format is used in the referenced example. The link in the network section of my console reads cpe_course_description_live_path.15 However, the controller, @cpe_event = CpeEvent.find(params[:id]), can't read the id

If I change it to: <tr data-link="<%= cpe_course_description_live_path(cpe_event, :format => 'html') %>" > it correctly sends the format. The params are now {"controller"=>"cpe_events", "action"=>"cpe_course_description_live", "format"=>"html"} and the network section of the console reads cpe_course_description_live_path.html

Bottom-line: Not passing the params or id to the controller.

Here's the view / table code:

<% @cpe_events.each do |cpe_event| %>
    <tr data-link="<%= cpe_course_description_live_path(cpe_event, :format => 'html') %>" >
       <td><%= cpe_event.id %></td>
       .....

Here's the jQuery:

$(document).ready(function() {
  $("tr").click(function() {
   window.location = $(this).data("link");    
  })
})

Here's the controller:

def cpe_course_description_live
  @cpe_event = CpeEvent.find(params[:id])
  @user = current_user
  redirect_to cpe_course_description_live_path(cpe_event)
end

Any help pointing me in the right direction would be appreciated. Thanks

Community
  • 1
  • 1
user3763682
  • 391
  • 1
  • 5
  • 17

1 Answers1

1

It is a bit hard to tell for sure, but I would assume this is because of our routes. It appears that the route you are using is not a member route, but a collection instead. That is most likely why you are getting your id after the "." in the url where the format type would normally show up.

You should never need to provide the "html" format as it is the default. If you post the appropriate routes from your routes.rb file I am sure this will show the problem

You can also see the url being produced from your cpe_course_description_live_path method by running the following from your console.

$ ~/code/my_app/rake routes | grep cpe_course_description_live

EDIT: After some research we figured out that there was an issue with a custom route. The route should have looked like this:

get '/cpe_events/:id/description_live(.:format)', to: 'cpe_events#cpe_course_description_live', as: 'cpe_course_description_live'

This allowed for the :id to be added to the params hash in the controller and accessed via params[:id]. This is essentially the difference between a collection route and a member route. In this cause we need the id of the member.

Andrew Griffith
  • 249
  • 2
  • 5
  • Thank you for responding. I removed the `:format => html` from the view, also removed a `post` route I'd erroneously added to the routes. Here's the routing: `GET /cpe_course_description_live(.:format) cpe_events#cpe_course_description_live` I'm now, again, getting `:format => 15`, 15 being the id. The params being passed are: `{"controller"=>"cpe_events", "action"=>"cpe_course_description_live", "format"=>"15"}` I believe my route should be similar to a `show` route`, e.g., `/cpe_course_description/:id(.:format)`, however, I don't know why that's not being generated. – user3763682 Jan 06 '16 at 18:56
  • Here's the controller code for the originating view: `def cpe_events_list @cpe_events = CpeEvent.where(:submitted_by == 'sponsor') .where('class_date >= ?', Date.today) end` If the issue is that I have a collection rather than an object, how do I resolve it. Thanks again, Doug – user3763682 Jan 06 '16 at 19:22
  • `get '/cpe_events/:id/description_live(.:format)', to: 'cpe_events#cpe_course_description_live', as: 'cpe_course_description_live'` – Andrew Griffith Jan 07 '16 at 04:59
  • I would use the above route to get what you want. the path starts with your plural model name. Then the required `:id`, followed by the action name. And lastly the optional `(.:format)`. The `()` indicate that a param is optional. The `:id` is not optional. So when you do `cpe_course_descroption_live_path(15)` the 15 will end up in the `params[:id]` in your controller. – Andrew Griffith Jan 07 '16 at 05:03
  • That worked. Thank you! Please edit your original answer and I'll accept it so that you get credit. Also, are there any readings other than RailsGuides: Rails Routing from the Outside In that you would recommend I spend time with? Thanks again, Doug – user3763682 Jan 07 '16 at 16:52
  • The rails guides are good, but I learn better via screencasts. Ryan Bates ran Railscast for a long time and put out some really good basic content. It is a bit outdated but a great place to start. There are only a few changes from Rails 3 to Rails 4. http://railscasts.com/episodes/231-routing-walkthrough & http://railscasts.com/episodes/232-routing-walkthrough-part-2 would be a good place to start. Then check the guides if you have any issues. This cheat sheet is also a nice quick overview and is up to date http://ricostacruz.com/cheatsheets/rails-routes.html – Andrew Griffith Jan 07 '16 at 19:54
  • I haven't seen the cheatsheet referenced before. I really appreciate all your help. I tried every angle I could think of, never suspecting routing. I've given you credit for the answer. Best, Doug – user3763682 Jan 07 '16 at 21:21