2

I've never encountered this problem before. I'm getting this error.

No route matches [GET] "/recipes/1/like"

Here is my routes.rb:

Rails.application.routes.draw do

  root 'pages#home'

  get '/home', to: "pages#home"

  resources :recipes do
    member do
      post 'like'
    end
  end
end

Here is my recipes_controller:

  def like
    @recipe = Recipe.create(params[:id])
    Like.create(like: params[:like], chef: Chef.first, recipe: @recipe)
    #flash message
    flash[:success] = "Your selection was sucessful"
    redirect_to :back
  end

Here is my html.erb file:

<%= render 'shared/page_title', title: @recipe.name.titleize  %>

<div class= "row">
  <div class="col-md-4 pull-right center">
    <%= gravator_for @recipe.chef, size: 200 %>
    <p>
      <h5>Brought to you by: <%= @recipe.chef.chefname.capitalize %></h5>
    </p>
  </div>
  <div class= "col-xs-8 col-md-8">
    <%= link_to "Edit this Recipe", edit_recipe_path(@recipe), class: "btn btn-success pull-right" %>
    <h3><%= @recipe.summary.capitalize %></h3>
    <div class="show_recipe">
      <%= image_tag(@recipe.picture.url, size: "300x200", class: "recipe-image") if @recipe.picture? %>
    </div>
    <div class ="well recipe-description">
      <p>
        <strong> Steps:</strong>
      </p>
        <%= simple_format(@recipe.description) %>
        <div class="pull-right">
          <%= link_to like_recipe_path(@recipe, like: true), method: :post do %>
            <i class="glyphicon glyphicon-thumbs-up"></i>
          <% end %> &nbsp&nbsp&nbsp&nbsp
          <%= link_to like_recipe_path(@recipe, like: false), :method => :post do %>
            <i class="glyphicon glyphicon-thumbs-down"></i>
          <% end %>
        </div>
    </div>
  </div>
</div>

<h5><%= link_to "Return to Recipes Listings", recipes_path, class: "btn btn-warning btn-small" %></h5>

I've explicitly added the HTTP POST request to my html.erb file

%= link_to like_recipe_path(@recipe, like: true), method: :post do %>

but rails is complaining that there is no GET route request, which I never created in my routes because I need a POST request for this particular section of the web app.

Rake routes:

     Prefix Verb   URI Pattern                 Controller#Action
       root GET    /                           pages#home
       home GET    /home(.:format)             pages#home
like_recipe POST   /recipes/:id/like(.:format) recipes#like
    recipes GET    /recipes(.:format)          recipes#index
            POST   /recipes(.:format)          recipes#create
 new_recipe GET    /recipes/new(.:format)      recipes#new
edit_recipe GET    /recipes/:id/edit(.:format) recipes#edit
     recipe GET    /recipes/:id(.:format)      recipes#show
            PATCH  /recipes/:id(.:format)      recipes#update
            PUT    /recipes/:id(.:format)      recipes#update
            DELETE /recipes/:id(.:format)      recipes#destroy

I am honestly lost. It seems that everything is in its right place.

Rails version:

Rails 4.2.5

I've defined the action, created the like model, nested the route under recipes, and explicitly requested for a post HTTP request in the html.erb page.

Any ideas would be great!

Cheers!

Alfonso Vergara
  • 1,086
  • 2
  • 9
  • 14
  • Your'e route error says this `No route matches [GET] "/recipes/1/like"` but your `rake routes` output only shows `like_recipe POST /recipes/:id/like(.:format) recipes#like` – Dan Rubio Nov 09 '16 at 18:29
  • Try inspecting the element in your browser. You can do this by right-clicking and choosing `inspect element` or something like that. What does the resulting html look like? – moveson Nov 09 '16 at 18:33
  • why you need a POST method? (instead of GET) – inye Nov 09 '16 at 18:44
  • @inye here is a great explanation of the differences between POST vs GEt http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get AS you can see I'm submitting data to the server, not requesting any data; that is why I need a POST request. – Alfonso Vergara Nov 09 '16 at 19:34
  • @AlfonsoVergara I know the difference, but why you use `link_to` instead a `submit`? – inye Nov 09 '16 at 19:37
  • @inye what do you mean? I am not submitting a form. I'm posting data to the database via the post method in my erb code. – Alfonso Vergara Nov 09 '16 at 19:39
  • @DanRubio I understand that. I don't need a GET request instead, I need a POST request, hence the reason I created a POST route ( in routes.rb), not a GET route. – Alfonso Vergara Nov 09 '16 at 19:47
  • Have you tried `link_to like_recipe_path(@recipe), {like: true, method: :post}` instead? – David Nov 09 '16 at 19:54
  • I hear someone tell create a link to POST is not recomend, but if you are sure about this. What if you use GET and in the controller check if is POST. According to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to you have to `...If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, patch?, or put?.` Also say `Useful for having links perform a POST operation in dangerous actions like deleting a record (which search bots can follow while spidering your site)` – inye Nov 09 '16 at 19:55
  • @henners66 I still get the same error. The route rails wants is a GET request, even though I am requesting a POST request. Not sure what the error is, – Alfonso Vergara Nov 09 '16 at 19:56
  • @inye why is it not recommend to request data as a POST request? Second, in my recipes controller, the like action is defined. I've already read the rails link that you provide. I am not deleting any records from the DB. No data is posted when I use a GET request because there is no data in the DB. I need to first POST data, then request it with a GET request. Please read, https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9 – Alfonso Vergara Nov 09 '16 at 20:02
  • @inye what would you recommend to post the data? – Alfonso Vergara Nov 09 '16 at 20:04
  • ok having played around a little i eventually got it to POST by specifically adding the first attribute (rather than having it derive from the link) - i.e. `link_to 'the text', like_recipe_path(@recipe), {like: true, method: :post}` - try that and see what happens – David Nov 09 '16 at 20:12
  • I do not have to much knowledge but someone say me that link is no to POST and I do not remember the why. – inye Nov 09 '16 at 20:22
  • @henners66 unfortunately, this creates an error prior to reaching the like action. Error: undefined method `stringify_keys' for "/recipes/1/like":String. I think it produced this error because in lieu of the first param ('the text') I'm displaying an icon (the erb line right below it). When I remove the first param it works, but I get the route error. – Alfonso Vergara Nov 09 '16 at 20:47
  • I have some buttons that work like this one, and they function just fine. The only obvious difference I see is I'm using PUT instead of POST. Maybe give that a try? You'll need to change in both your `link_to` and your `routes.rb`. Make sure to restart the server after you make the changes. – moveson Nov 09 '16 at 21:24
  • @moveson are the buttons from bootstrap? – Alfonso Vergara Nov 09 '16 at 21:27
  • 1
    Yes, my entire app is styled with bootstrap. – moveson Nov 09 '16 at 21:31
  • @moveson I still get the same error. The button_to method works but I lose my bootstrap icons. I've used post before in link_to, so i have no idea why it's not working now. It may be that I'm using cloud9 and it's somehow blocking js code, but that's unlikely. – Alfonso Vergara Nov 09 '16 at 21:31
  • @moveson does this link work for you? https://recipe-manager-slothc0der.c9users.io/ – Alfonso Vergara Nov 09 '16 at 21:32
  • It takes me to a sign-in screen. You should take a look at my project repo and see if that helps you. See the link in my answer below. – moveson Nov 09 '16 at 21:37

4 Answers4

1

Here's the relevant code from my working project with a similar arrangement.

# /views/events/splits.html.erb:

<%= link_to "Add",
   associate_splits_event_path(id: @event.id, split_ids: [split.id]),
   :method => :put,
   :class => 'btn btn-xs btn-success' %>

# routes.rb

  resources :events do
    member { put :associate_splits }
  end

If it's helpful to see it in context, feel free to poke around the repo. Here's a link to the view: https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb

moveson
  • 4,620
  • 1
  • 11
  • 31
  • Thanks, I'll check it out – Alfonso Vergara Nov 09 '16 at 21:40
  • Our relevant code is almost identical in format - from the routes to the html.erb file. I do believe my icon tag is causing the app to break. I'm going got remove it and add in "text" for the first param of link_to. – Alfonso Vergara Nov 09 '16 at 21:44
  • As I expected, when i remove the icon tag and add in text for the first param, it works; this has blown my mind away; it really doesn't make any sense. – Alfonso Vergara Nov 09 '16 at 21:47
  • 1
    Use [this method](https://www.topdan.com/ruby-on-rails/ajax-toggle-buttons.html) instead. Better in every way. I'm using it for my follow/unfollow buttons, and it is slick. – moveson Nov 09 '16 at 23:01
0

Try with

<%= link_to "LIKE", like_recipe_path(@recipe) ,  method: :post %>

Other option:

<%= link_to '<i class="glyphicon glyphicon-thumbs-up"></i>'.html_safe , like_recipe_path(@recipe ,  like: 'value' ) ,  method: :post %>
inye
  • 1,718
  • 1
  • 22
  • 28
0

While rails link_to does allow links to make requests with a method other than GET, it is not the normal behavior of links and relies on Javascript to make a form which gets submitted behind the scenes. Rails uses jquery-ujs for this, so make sure you haven't disabled it.

That said, making POST requests is normal behavior for forms, so you could also try using button_to instead, and simply styling the button to look like a link if necessary.

<%= button_to "Like",  like_recipe_path(@recipe), method: :delete, like: true  %>

Additional Info:

https://github.com/rails/jquery-ujs

http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

  • I tried the button_to but then my icons are not displayed. I have already checked that I have JS on, which I do. – Alfonso Vergara Nov 09 '16 at 20:59
  • Also, I've included the jquery gem into the gemfile for production and development. – Alfonso Vergara Nov 09 '16 at 21:01
  • Fred, I'm a little confused; I am not submitting a form. – Alfonso Vergara Nov 09 '16 at 21:11
  • Your icons not being displayed does not sound like a side effect of `button_to`, since they are a seperate tag, is it possible anything else changed? As for submitting a form, if you're making a post request with `link_to` or `button_to` somewhere a form is being submitted, whether it's behind the scenes the with javascript for `link_to` or directly on the page for `button_to`. – Fred Sladkey Nov 09 '16 at 21:37
-3

You need to add a GET route:

resources :recipes do
    member do
      get 'like'
      post 'like'
    end
  end
Dan Rubio
  • 3,965
  • 5
  • 34
  • 74