1

Hi I'm new to rails and MVC but I'm trying really hard to learn. Right now I'm using AASM to make a transition from in_draft to published. I'm being able to make the change in rails console but when trying to use a link_to I got the error in the question

`#/app/views/welcome/dashboard.html.erb
<% if article.may_publish? %>
<%= link_to 'Publish', '/articles/#{article.id}/publish', method: :put, class: "alert-link" %>
<%end%>

This is mi route

put '/articles/:id/publish', to: 'articles#publish'

And my articles_controller publish method

def publish
    @article.publish!
    redirect_to @article
end
jean182
  • 3,213
  • 2
  • 16
  • 27

2 Answers2

1

you are really, really close! You need to use double quotes to be able to infer using #{}.

<%= link_to 'Publish', '/articles/#{article.id}/publish', method: :put, class: "alert-link" %>

should be:

<%= link_to 'Publish', "/articles/#{article.id}/publish", method: :put, class: "alert-link" %>
Jeremy
  • 725
  • 6
  • 11
  • Thanks that was the error, you helped me a lot, can you explain me the difference between the ' ' and " ". I'm getting used to use the ' ' but I didn't know about the other one. – jean182 Nov 21 '16 at 01:16
  • The only difference, from my understanding, is that, assuming article exists, using "#{article.id}" works, but using '#{article.id}' does not work. You can also do things like put single quotes inside of double quotes, where double quotes inside of single quotes might not work. For example "'The best article has the id of' + #{article.id}" – Jeremy Nov 21 '16 at 18:20
0

Welcome to rails. I would like to suggest you to use member for adding a RESTful put action. Rails routing

resources :articles do
  put :publish, on: :member
end

To resolve your current given route problem, Please as: :public_article.

put '/articles/:id/publish', to: 'articles#publish', as: :public_article

Enjoy

Nitin Srivastava
  • 1,414
  • 7
  • 12