How to link to controller method - ruby-on-rails

I'm following this answer on how to clone a record.
I can't though workout how to phrase the link and route it.
It is in my #miniature show view so I thought it should be something like
<%= link_to 'clone', :controller => :miniatures_controller, :action => :clone %>
and the route
match 'clone', to: 'miniatures#clone', via: 'get'
but this is clearly wrong. I am using #miniature in place of the above answer's #prescription.

What if you just use clone_path:
<%= link_to 'clone', clone_path %>
Cause rake routes shows just clone route. It works with the same routes.
If you are not satisfied with route and you should pass parameters (like miniature_id), add member to your resource (probably nested), like:
resources :miniatures do
member do
get 'clone'
end
end
This will be clone_miniature_path where you should pass #miniature:
<%= link_to 'clone', clone_miniature_path(#miniature) %>

Related

Rails Form_tag syntax error

I get an error when I run this code. I want to make a button that redirects to an action from pages_controller.
Submultimi.html.erb
<%= form_tag({:controller => '/pages_controller', :action => 'calculeaza'}, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
pages_controller.rb
def Submultimi
end
def Combinari
end
def Permutari
end
def calculeaza
puts "YAY"
redirect_to '/combinari'
end
Error message: No route matches {:action=>"calculeaza", :controller=>"pages_controller"}
routes.rb
Rails.application.routes.draw do
get '/submultimi' => 'pages#Submultimi'
get '/combinari' => 'pages#Combinari'
get '/permutari' => 'pages#Permutari'
end
If you get an error you must include the error message in the question. Anyway the controller name is obviously wrong so this must be the problem. The controller should not include the "/" nor the "_controller".
<%= form_tag( { :controller => 'pages', :action => 'calculeaza' }, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
Your routes are also wrong:
there's no calculeaza method in routes
methods should be lowercase in routes and controller
Thanks to Phlip for the correction about the controller name :)
As Pablo said, remove the / from your controller name.
Your error message says there is no route defined. That means you haven't correctly told rails what to do with your form's post request; it's trying a route that doesn't exist.
You've got a few things going wrong. Your action names are capitalized in routes.rb, but your method names (at least the one you've linked) is not. They're case sensitive, convention is all lowercase. Also, you don't have a route defined for calculeaza. You need one, in routes.rb add (something like, I haven't tested any of this):
post '/calculeaza/' to 'pages#calculeaza'
If you want to see your currently defined routes, run rails routes in a terminal, and to use it in code append _path to the prefix verb. You end up with something like:
form_tag calculeaza_path do
You may want to read the rails routing guide, especially the parts about resourceful routes.

No route matches [PUT] "/articles" but I included the put route in routes

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
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" %>
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

acts_as_votable gem routes error

i'm new to rails and trying to get upvotes for questions working using the acts_as_votable gem. I am getting the following error telling me i have no route matches:
No route matches [GET] "/questions/1/like"
Here is my upvote method in my questions_controller.rb:
def upvote
#question = Question.find params[:question_id]
#question.liked_by current_user
redirect_to #questions
end
My routes.rb file:
resources :comments do
resources :questions
member do
put "like", to: "questions#upvote"
end
end
and my upvote button:
<%= link_to "Upvote", like_question_path(#comment, #question, method: :put) %>
Thanks for the help!
The path name like_question_path is incorrect. It should contain at least "comment", something like "like_comment_question_path". Please consult your $rake routes for accurate name.
By the way, is there any reason you need to use put? In my opinion this action is not to change existing data but add a new one, so 'POST' should be more appropriate.
Try this:
routes.rb
resources :comments do
resources :questions do
put "like", to: "questions#upvote"
end
end
Upvote button:
<%= link_to "Upvote", comment_question_like_path(#comment, #question), method: :put %>
You need the correct path, and also the method for link_to to use goes after the second parameter.
The method option should be outside of the named route, like this:
<%= link_to "Upvote", like_question_path(#comment, #question), method: :put %>
Also, what #Billy Chan said.

Route to controller action

I created an form_tag form:
<%= form_tag(set_image_dokumente_path) do %>
<%= text_field_tag :shit,'', data: {autocomplete_source: search2_patients_path}, :class => "shit" %>
<% end %>
I try to route to set_image action of dokumente controller, but i get the error:
undefined local variable or method `set_image_dokumente_path' for #<#<Class:0x711ff60>:0x762d578>
By default my form_tag goes to dokumente controller index action!
My routes:
resources :images
get "dokumente/index"
post "dokumente/index"
match 'patients/list' => 'patients#list'
resources :patients do
collection do
get :search2
end
end
How do i have to change it?
You can add the as: parameter to you route in order to create a named path.
For example:
post "dokumente/index", as: 'set_image_dokumente'
or similar, I'm not sure what you are trying to achieve, but I hope you get the idea :)
More info:
http://guides.rubyonrails.org/routing.html#generating-paths-and-urls-from-code

Best way to update an ActiveRecord attribute from a link

I have a Model with an attribute votes. I have a link in a view that needs to increment the value of votes - what is the best way to do this?
I am currently trying a link like:
<%= link_to 'Up', '#', :method => :voteup %>
and a voteup method in the model_controller but this isn't working.
I think the best way would be this:
In config/routes.rb:
resources :quotes do
member do
post :upvote
end
end
And your link:
<%= link_to 'Up', upvote_quote_path(#quote), :method => :post %>
Note that we use a POST request, which is more appropriate than a GET request when modifying a record.
:method is only supposed to be used to specify between POST, GET, DELETE, and PUT requests. Your second parameter of link_to should be the action you want to execute in your controller.
<%= link_to "Up", :action => :voteup %>

Resources