How to set an edit link? - ruby-on-rails

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

Related

Putting Devise User ID in Route

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

Rails not routing edit method using /edit

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

Rails Routing and link_to

It's a little late here so maybe this a trivial question where I'm missing something simple. But when I click a button (with link_to) I created the following gets appended to my URL:
%23<ActionView::Helpers::FormBuilder:0x3ef1fd8>
Why is this, and how can I prevent this? Again, I apologize if this is a shallow question. I can post more information regarding routes and whatnot if that is needed.
Thanks!
Edit: More information as requested.
View:
<%= link_to "Index", welcome_path(f), :class => 'button' %>
with f being part of a form_for loop. I think I'm passing the wrong parameter but I'm unsure.
Relevant Route:
get "index" => 'welcome#show', :as => 'index'
Update:
Thanks for the help everyone. I ended up getting it working by pluralizing my controller (I don't know why I didn't have that before) and utilizing welcome_url instead. That seemed to do the trick.
Check out the very first example and paragraph in the Rails API docs for ActionView::Helpers::FormBuilder:
<%= form_for #person do |f| %>
Name: <%= f.text_field :name %>
Admin: <%= f.check_box :admin %>
<% end %>
What this is saying is that f represents an instantiated FormBuilder object that you are passing to the welcome_path method in your link_to helper.
Typically, you would not mix #index and #show in your routes. Depending on what you want to use the WelcomesController for, you might actually want to route your root_path to welcome_index:
get "welcome/show" => 'welcome#show', :as => 'welcome'
root 'welcome#index'
You should run: $ rake routes in the terminal to get an idea of path view helpers that you can use in your app.
Maybe you're trying to send users to a personalized welcome page. You could have something like this for your corresponding link_to helpers would look best like this:
<%= link_to "Show", welcome_path(#user.id), :class => 'button %>
<%= link_to "Index", root_path, :class => 'button' %>

Rails button_to fails with path doesn't exist for a path that exists

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.

How do I link to the create function in my controller in Ruby on Rails?

I've got this line in my routes.db file:
map.resources :things
I'm trying to create a link that will create a new thing. So far I've got
<%= link_to "add thing", things_path (:thingName => key)%>
But I'm getting this error message:
Unknown action
No action responded to index. Actions: create and new
How do I do the link_to line so that it links to the create method instead of the index method? Thanks for reading.
Do you want to link to the new or the create action? The new action is:
<%= link_to "add thing", new_thing_path %>
The create action would not make sense here, since you don't have any data to inject into the new object? Unless I'm missing something...
You probably dont want to create a resource through a link like that. Links are HTTP GET requests, which can be cached, and search engines will follow that link, resulting in database records being created incorrectly. You should only use HTTP POST requests to create a resource. To do that you need a form. If you already know the data to pass, you can use hidden_field to pass additional data
<% form_for Thing.new(:thing_name => key ) do |f| %>
<%= f.hidden_field :thing_name %>
<%= f.submit %>
<% end %>

Resources