I need create a link to users/id/microposts where id is the user id. I was wondering how I could do that with link_to?
The HTML code is I am using for the moment is :
<strong>Microposts</strong> <%= user.microposts.count %>
Which renders the following :
Microposts
In routes.rb, you should have something like this:
match '/users/:id/microposts' => 'microposts#index_by_user', :as => :microposts_by_user
and in view, you can do like this:
<%= link_to "Microposts", microposts_by_user_path( #user) %>
Assuming that you have your routes setup correctly, that would be a nested route for the microposts, In rails 3 it would be
resources :users do
resources :microposts
end
Then in your view, with link_to you can
<%= link_to "Microposts", user_microposts_path(user) %>
This might be current_user, #user ... whatever user object you want the microposts, it might be from
#users.each do |user|
<%= link_to "Microposts", user_microposts_path(user) %>
end
OR
<%= link_to "Microposts", user_microposts_path(current_user) %>
EDIT: Added in the user object I forgot to passin and some examples with it.
Related
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' %>
I'm in need of some help regarding setting up the routing (I think that is the problem) for a view that is set up with bootstrap tabbed navigation. What I want to do is set up a form in each tab. Sounds easy enough.
However I can't figure out how the routing works when you want to submit two different actions (one to save to the db, and the other to get info from the db), each from their own tab. I can get it all to work perfectly if I give the "get_users" controller action a view, but when I try to bring it back together on the same page, just in different tabs, it goes a little awry and I'm not sure how to route correctly.
The two controller actions I have are:
class Users::RegistrationsController < Devise::RegistrationsController
def user_accounts
#user = User.new
end
def get_users
#users = User.search(params[:search]).paginate
(:per_page => 20, :page => params[:page] )
end
end
EDIT ------------------------------------
Include routes.rb - excluding the extra devise related routes. Can get the create_internal_user action working not the other. I understand that the routes are
incorrect as rails could never understand them. The first is correct, the second is simply what I would imagine it to look like if it were possible
# Authentication
as :user do
#add new internal users
get '/useraccounts' => 'users/registrations#user_accounts', as: 'new_internal_user'
post '/useraccounts' => 'users/registrations#create_internal_user', as: 'internal_user'
**# searching users - this is where I am uncertain**
get '/useraccounts' => 'users/registrations#get_users', as: 'get_internal_user'
post '/useraccounts' => 'users/registrations#get_users', as: 'getting_internal_user'
# other devise routes here
end
The view that renders them looks like this and renders two partials:
<div class="container">
<h1>User Accounts</h1>
<ul class="nav nav-tabs">
<li class="active">Add User</li>
<li>Edit User</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane fade in active" id="adduser">
<%= render 'users/registrations/create_internal' %>
</div>
<div class="tab-pane fade in" id="getusers">
<%= render 'users/registrations/get_users' %>
</div>
</div>
</div>
The two partials
_create_internal.html.erb
<%= form_for(resource, as: resource_name,
url: new_internal_user_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<% for role in User::ROLES %>
<%= check_box_tag "user[roles][#{role}]", role,
#user.roles.include?(role),
{:name => "user[roles][]"}%>
<%= label_tag "user_roles_#{role}", role.humanize %><br />
<% end %>
<%= hidden_field_tag "user[roles][]", "" %>
<div class="col-md-10 center"><%= f.submit "Create User",
class: "btn btn-primary"%></div>
<% end %>
_get_users.html.erb
<%= form_tag({:controller => "users/registrations", :action => "get_users"},
method: :get) do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<div class="center" id="users">
<% if defined?(#users) %>
<%= render 'users/registrations/searchresults' %></div>
<%= will_paginate #users %>
<% end %>
I've spent many many hours trying to figure this out and just haven't been able to. Very new to Rails so this may be an easy question for someone. I've had a look at using AJAX, and resourceful routing, but as I'm still new I'm finding it a bit tough to wrap my head around it all at the one time. This is my first post on SO so apologies if I'm missing anything (please let me know).
Let's say your controller is UsersController and assuming your routes are not nested, this is how you create named routes:
get '/useraccounts', to: 'users#get_users' ## this route maps to the get_users action
post '/useraccounts' to: 'users#create_internal_user' # this route maps to the create_internal_user action
EDIT:
the format is controller_name#action_name. So make sure to replace users with your controllers name in plural
UPDATE:
if your controller's name is RegistrationsController
try:
get '/useraccounts', to: 'registrations#get_users'
post '/useraccounts' to: 'registrations#create_internal_user'
Looking at it, I think your error will likely be from your _get_users.html.erb partial.
Firstly, you're using form_for - why? Your use of form_for basically means you have to use the data inside #user to make it work, which I think will be causing the problem
I would use a form_tag as this does not persist your data:
<%= form_tag({:controller => "users/registrations", :action => "get_users"}, method: :get) do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<% if defined?(#users) #-> this might need to be passed as a local var %>
<%= render 'users/registrations/searchresults' %></div>
<%= will_paginate #users %>
<% end %>
Routes
Your other problem is to do with your routes:
get '/useraccounts' => 'users/registrations#user_accounts', as: 'new_internal_user'
post '/useraccounts' => 'users/registrations#create_internal_user', as: 'internal_user'
**# searching users - this is where I am uncertain**
get '/useraccounts' => 'users/registrations#get_users', as: 'get_internal_user'
post '/useraccounts' => 'users/registrations#get_users', as: 'getting_internal_user'
Tell me how you expect Rails to know the difference between get '/useraccounts and get /useraccounts? The fact is it can't
You'll need to split up the routes so they don't all use the same path. I would do this:
get '/useraccounts' => 'users/registrations#user_accounts', as: 'new_internal_user'
post '/useraccounts' => 'users/registrations#create_internal_user', as: 'internal_user'
**# searching users - this is where I am uncertain**
get '/search' => 'users/registrations#get_users', as: 'get_internal_user'
After much frustration I have solved my own problem. With help from Rich Peck I realised that I was essentially creating a view with two forms on it, and that the tabbed navigation didn't really mean anything to the problem.
So I really only needed the two routes:
# #add new internal users
get '/useraccounts' => 'users/registrations#user_accounts', as: 'user_accounts'
post '/useraccounts' => 'users/registrations#create_internal_user', as:
'create_internal_user'
And then in the controller just changed the user_accounts action to look like this:
def user_accounts
#user = User.new
if params[:search_button]
#users = User.search(params[:search]).paginate(:per_page => 15, :page => params[:page] )
end
end
Came about this discovery thanks to this question/answer here:
form_for with multiple controller actions for submit
In the end the problem wasn't what I thought it was and ended up being simple. Was certainly an interesting learning journey. Thanks for your help again Rich Peck.
Hi how can I edit this code so when I click on search, I am redirected to specifed page not stay on the same?
<%= form_tag products_path, :method => 'get',:id => "products_search" do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %> </li>
<% end %>
You can do it like this:
<%= form_tag "http://www.stackoverflow.com/questions", :method => 'get',:id => "products_search" do %>
Put whatever you need in place of that URL.
for more info please look at the form_tag helper documentation
But if you need actually to be trough that path you were using, you have to use a redirect or a forwarding.
For using a forwarding just call the name of the next action near the end of that action yuo are calling.
Something like:
def another_action
...
end
def product
...
another_action
end
For using a redirect just do it like:
def products
redirect_to another_action
end
I am trying to accomplish something like this:
I am creating a simple blog. I have set up categories for my blog.
I want that when my user goes to posts/index, he sees a list of all categories.
Example:
Text
Image
Upon clicking on a category, my user gets redirected to the posts/new page, where the category_id field will by transmitted through a hidden_field.
So my code right now is:
in posts/index
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category => c.id) %><br />
<% end %>
and in my posts/_form i'm trying to do something like this
<%= f.hidden_field :category_id, :value => params[:category_id] %>
which is not working though, because the html output is
No value is being passed.
What is the correct way to proceed here?
Thx!
At first glance it looks like a simple mistake mixing up the param names category and category_id.
Try this:
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category_id => c.id) %><br />
<% end %>
Also, from what i can understand in your code, it seems a post belongs to a category. In such case, you could nest routes from one in another, and paths for creating nested object would become accessible, such as new_category_post(#category).
The routing would look like that:
resources :categories do
resources :posts
end
You can read about this matter here: http://guides.rubyonrails.org/routing.html
I have model Account, which has n, :transfers.
In the list of all available accounts
<% #accounts.each do |acc| %>
<%= acc.name%>
<%= acc.value%>
<%end%>
#in the controller
#accounts = #owner.accounts.all( :name.not => nil )
I need to add an option of creating of a transfer from this account, something like that:
<%= form_for #new_transfer, :url => {:controller => "transfers", :action=>"create"} do |trans_form|%>
<%= trans_form.text_field :amount %></br>
<%= trans_form.text_field :to %></br>
<%= trans_form.text_area :comment, :rows=>5 %></br>
<%= submit_tag %>
<%end%>
#in controller
#new_transfer = Transfer.new()
How should i declare the parent for this new children item? I've tried something like <%= form_for acc.#new_transfer, #new_transfer = #accounts.each.transfers.new(), <%= form_for acc.transfers or fields_for, but none of them seems to work. Will be thankful for any advice.
You need to look at the routes, you should consider having a nested route for the transfer model under account, and at the controller level scope the transfer calls to that account.
For example the routes
resources :accounts do
resources :transfers
end
Then in your transfers controller with a before filter to find the account (for all but index actions) by account_id:
def create
#transfer = #account.transfers.new params[:transfer]
...
end
View:
form_for #account, #transfer do ..
More info on nested resources:
http://guides.rubyonrails.org/routing.html#nested-resources
Try,
form_for(acc, acc.transfers.build) do |f|
.....
end