I'm currently using devise with the following link_to url to "submit" a Resource (resources_controller, resource.rb model)
This is in the menu:
<li><%= link_to "Submit Resource", :action => 'new', :controller => 'resources' %></li>
It works fine if I'm not on a devise login page (user signup, user login, etc)
otherwise it changes the url from /resources/new
to this:
http://localhost:3000/assets?action=new&controller=devise%2Fresources
The only reason i linked it using the first piece of code above is because I'm not sure if theres a better way to link to a particular REST action directly for a given controller (I'm not using :index)
Use <%= link_to "Submit Resource", new_resource_path %>. Before use it you should make sure if you have line resources :resources line in your routes.rb
Related
The forum_posts controller is located in app/controllers/forum_threads/forum_posts_controller.rb
I don't know if I have to call forum_threads:forum_posts in the link_to.
Controller:
http://pastebin.com/t9vuyxdP
HTML:
http://pastebin.com/LextuZ74
On a side note, how do you add a button to the link_to? I've tried adding :class => "button" at the end, doesn't cause an error but still just shows a link not a button.
If you want to use link_to with older-style controller/action/id arguments:
<%= link_to "Edit", :controller => "forum_threads/forum_posts", :action => "edit", :id => forum_post.id, :forum_thread_id => #forum_thread.id %>
But Rails prefer newer RESTful routes whenever possible. If you define your controller correctly in routes.rb, you can write link_to like this:
<%= link_to "Edit", edit_forum_thread_forum_post_path(#forum_thread, forum_post) %>
UPDATE 18/11/2015: You forgot to include forum_thread_id in link_to as I saw your route require forum_thread_id and id param to make it work.
For more information on link_to, refer to: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
I'm looking to put the users ID within my applications URL. I have devise all set up with a default configuration, what I'm trying to accomplish is to have a URL when the user is logged in as the following:
application.com/userid/page
and when they're not logged in
application.com/page
Is this possible with Devise?
Thanks in advance.
You just need to create a route that looks for an :id param, then make sure you pass an :id param when navigating to the route. Here's a simple example:
routes.rb
get '/page', to: 'pages#page', as: :guest_page
get '/:id/page', to: 'pages#page', as: :user_page
page.html.erb
<% if user_signed_in? %>
<%= link_to "page for logged in user", user_page_path(current_user) %>
<% else %>
<%= link_to "page for guest user", guest_page_path %>
<% end %>
Also, if have many routes, you will want to extract this logic outside the view into a helper method or decorator.
Actually is more routing than devise.
Scope your routes
scope path: ":account_id", as: "account" do
resources :projects
end
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html
I am using the following code to attempt to link the customer's name to the edit method.
<%= link_to "#{customer.name}", customer, method: :edit %>
But when I click on the name it opens up /customer/1 instead of /customer/1/edit
If I manually enter /customer/1/edit it opens the page correctly. The worst part is that it used to do it correctly and then stopped.
How do I get my edit method to use the /edit again?
Link will be like this..
<%= link_to "#{customer.name}", edit_customer_path(customer) %>
if you want to mention method in link_to, you can do it as,
<%= link_to "#{customer.name}", :controller => 'customers', :action => 'edit' %>
I'm using Rails 4.0.10. I have a show and edit view for a model. The show url is just model/id, and the edit url is model/id/edit. What I can't figure out is how to create a link on the show page that redirects to the edit page.
Here's what I've tried so far:
<%= link_to "edit", model_edit_path(model) %>
The correct way is (swap model and edit around) :
<%= link_to 'Edit', edit_model_path(model) %>
The RESTful default routes are the following:
#index => models_path
#new => new_model_path
#edit => edit_model_path(:id)
#show => model_path(:id)
If you want to see all your available routes, use rake routes in your console
Writing my first, very simple Rails application, a simple admin app to track work for one of our departments. The generated index page for people has a link_to on it to add a new person. I tried to change that to button_to and it fails saying the path /people/new doesn't exist, though obviously it does since link_to goes to the same place.
I'm using Rails 3/Ruby 1.9.2. I have this code on my /app/views/people/index.html.erb page:
<%= link_to 'New Person', new_person_path %>
<%= button_to "New", :controller => "people", :action => "new" %>
The link_to works. The button_to fails with this:
Routing Error
No route matches "/people/new"
Also tried just
<%= button_to 'New Person', new_person_path %>
Same error. Odd.
button_to defaults to the post method. Try putting :method => :get in there. This is why link_to works.
There's a good explanation for this, as always :)
link_to uses GET as default, where button_to uses POST. And there's no POST route that matches, only a GET route.
If you want to use button_to, you can add :method => :get to your buttons params and it will use GET.
Did you set up your routing options in config/routes.rb? Check if you have this in your routes.rb file:
resources :people
Check this guide for more informations about how routes work.
Is your button_to inside a form? button_to creates a form of its own so this would create a form within a form and likely break routing.