Rails Routing and link_to

133 views Asked by At

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.

1

There are 1 answers

1
sealocal On

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