Profile a Rails Controller Action

Display link to a controller action on a user's profile if logged in

You can wrap the button in an if block:

  <% if current_user == @user %>

<%= link_to "Add work", { :controller => 'works', :action => 'new' }, :class => 'btn btn-primary btn-small' -%>

<% end %>

Rails - Editing User and Profile Models from separate Settings Controller

When you define the form as <%= form_with(model: @profile, local: true) do |form| %>, it is equivalent to <form action="/profiles/1" method="post" data-remote="false">. You need to tell Rails to map the form to the custom url like so

<%= form_with(model: @profile, url: update_settings_profile_path, local: true)

And you need to whitelist username and surname in the profile_params in order to reflect the changes in the DB.

def profile_params
params.require(:profile).permit(:username, surname)
end

How does Rails know which controller's show action to use in this example from Rails Guides?

That documentation is somewhat misleading, isn't it. It is a poor example. The comment about passing symbols to match is a general comment, and should be outside the section on singular resources.

Indeed if you try it stand-alone, you will get the following error when starting Rails or running rake routes:

rake routes
rake aborted!
missing :controller

So you would have to add a :controller option for that to work outside of a resource declaration:

get 'profile', to: :show, controller: 'users'

The syntax they specify IS valid inside of a resources or resource declaration, e.g.:

resources :user do
collection do
get 'profile', to: :show
end
end

or

resource :user do
get 'profile', to: :show
end

However both those examples generate different routes from the prior example. So again, I think the comment is misplaced.

Controller Show Action

It looks like you're not setting @profile in the actions that actually use the layout, so it's coming up as nil, and the router needs an id to generate the route.

You need to be setting @profile to something in any action that renders with the application layout. This is possibly a job for a global before_filter.

No route matches {:action=show, :controller=profiles, :user_id=nil}, missing required keys: [:user_id]

You need to pass user_id in link, use this instead

<%= link_to 'Show user profile', user_profile_path(user, @profile) %> 

Since it's a nested route you need to pass both the objects/ids.

Hope that helps!

How can I profile a request in Ruby on Rails?

ruby-prof is the way to go. Here's a howto, How to profile your Rails and Ruby applications with ruby-prof.

If you use it in combination with a visualisation tool such as kcachegrind, you can easily separate the code that is your application code from the framework code.

I gave a talk on these tools at my local Ruby-users group a while back: Making your rails app kick ass with ruby-prof and kcachegrind.

User profile pages with devise - Routing to show action

You just need to pass User instance as a parameter to link_to if you want it to link to given user's show page. So if you want to link to currently logged in user's profile in Devise, you just need:

<li><%= link_to "Profile", current_user %></li>


Related Topics



Leave a reply



Submit