So far, I know that in Rails I can simulate a PUT request using a form which has a hidden input with name='_method' and value='put', but I'm interested in simulating that for a link.
How can I have a link in a view that would fit this route:
match '/article/:id/publish', :to => 'article#publish', :via => :put
The docs for link_to say you can specify a :method option that creates a form that is submitted on clicking the link.
link_to "Publish!", publish_article_path(article), :method => :put
Not sure what your route helper method would be (I assumed publish_article_path - you should be able to figure it out with rake routes from the command line. The :method is the important part that will do the magic you want.
Related
While there are similar questions on this error, most relate to how Rails route handle pluralisation, whereas this is different.
I have a link on the 'show' view page within a controller called MemberPage which I need to link to the 'new' action within a controller called Post
<%= link_to ((content_tag :i, "", :class => "icon-pencil") + content_tag(:span, 'create')), new_members_community_post_path %>
Within routes I have
resources :post
which produces the following related line in rake routes
new_members_community_post GET /members/community/post/new(.:format) members/community/post#new
Hovering over the link shows
127.0.0.1:3000/members/community/post/new
Clicking on it produces the error
No route matches {:action=>"show", :controller=>"members/community/member_page"}
Rails is matching both the wrong controller and the wrong action from that shown in rake routes
I've tried creating explicit match rules in routes.rb, eg (within the members/community namespace)
match '/new-post' => 'post#new'
and also replacing the link_to path with
:controller => :post, :action => :new
but cannot find anything that works
Rails has generated this action url for my form:
<form action="/auth/failure?action=update&controller=users"...
I'm authenticated to the website, even If I shouldn't be, it would redirect somewhere as I understand Auth.
My Form code:
<%= form_for #user, :url => { 'controller' => 'users', 'action' => 'update'} do |f| %>
Thanks!
There is two thing you have to know
Ruby makes difference between symbols (:controller, :update) and strings ('controller', 'update').
You do not need to specify :url in this case.
So, for your current code, the correct line is simple:
form_for #user do |f|
If #user is a new record, form_for will point to create action of your UsersController, if #user is an existing record, form_for will point to update action automatically. Rails is smart enough to do this :-)
If you really want to use :url attribute of form_for, you have two option:
Use routing helper methods: form_for #user, :url => user_path(#user), :method => :put
Use a correct path hash: form_for #user, :url => { :controller => :users, :method => :update, :id => #user.id }
Rails uses REST style for building urls if you use resources :users in config/route.rb (and I recommend you to use that), and it have two thing you need to know:
- Collection is a group of entities (in your case, users)
- Member is one entity
So collection URL is something where you can expect multiple entities, member URL is something where you can expect only one entity.
You must use :id when you describe update action with URL-hash, because update can done only on member, not on collection. So you have to build a member URL with a special HTTP method (PUT) to clarify, what do you want to do.
When trying to use user authentication I get the following error: "NoMethodError in Viewer#show". And it addresses the error to <%= #page.body.html_safe%> in app/views/viewer/show.html.erb:1:in '_app_views_viewer_show_html_erb__685858346_34780128', which is only one line code by now.
But, when I call login page on browser address bar like: :3000/session/new, it comes Up. Which is not happening with :3000/session/destroy.
It seems that something related to the route is not working properly because, on the other hand, when I call a page on views/layouts/application.htm.erb like <li><%= link_to 'Home', {:controller => 'viewer', :action => 'show', :name => 'home'} %></li> it works, and if I switch to <li><%= link_to 'Home', view_page_path('home') %></li> it gives a similar error.
How can I solve that?
Your use of view_page_path('home') assumes that there is a named path view_page. Changing
get "/:name" => 'viewer#show'
to
get '/:name' => 'viewer#show', :as => :view_page
should fix that.
Secondly when using route helpers with named parameters you need to specify the name so Rails knows what parameters should be used. Change view_page_path('home') to view_page_path(:name => 'home').
And finally a NoMethodError for <%= #page.body.html_safe%> suggests to me that either #page or #page.body is nil.
I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.
I have a back button setup on my Show Event page and its simply:
<%= button_to "Back",events_path %>
When i click this to return to my events index I get the message The action 'create' could not be found for EventsController. That's true I dont have a create action but why is it looking for one? It should just be returning me to the index of events and I'm not passing any parameters correct?
I tried adding :only => [:index, :show] to my routes entry but that didn't solve the problem. Any other suggestions or could you explain why it is trying to create? Thanks!
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
The default method is POST. You want:
<%= button_to "Back", events_path, :method => :get %>