Push user.id to another Controller

Good morning everyone, I have a question, if anyone can answer it would be great. I’m trying to send the user.id from the “users/show.html.erb” to “photos_controller.rb”, but I’m not sure how to do it. I can send the user information to the Model but I’m not sure is that is correct. Below is the code that I’m using: “users/show.html.erb” → <%= link_to ‘Add Photo’, :method => Photo.add(@user) %> **“photo.rb” → ** def self.add (name) puts ‘Adding photo’ print name.name end Thi other thing that I’m trying is the next one. Could you tell me if it is correct? **“users/show.html.erb” → ** <%= link_to ‘Add Photo’, new_photo_path, :id => @user.id %>

Thanks & Best regards

Alfred

I didn’t really get the scenario there. If you don’t mind in explain better the flow you want to achieve…

**“users/show.html.erb” → ** <%= link_to ‘Add Photo’, new_photo_path, :id => @user.id %>

And about what you’ve been trying, I can say that it depends on how your routes look like. Is there any association between Users and Photos? If so, are the routes nested? Have a look on section 2.7 Nested Resources of this link**The guide is your mate**

You do it like this:

<%= link_to “Pic”, your_route(user_Id: user.id) >

Then in your controller you can access the user id with: params[:user_id]

Hope it helps

Alfredo Barrero wrote in post #1143117:

Good morning everyone,

I have a question, if anyone can answer it would be great. I'm trying to send the user.id from the "users/show.html.erb" to "photos_controller.rb", but I'm not sure how to do it. I can send the user information to the Model but I'm not sure is that is correct.

Speaking generally, users are authenticated (login form) and the id of the user is stored in the session so that each controller can access the user directly from the user's session.

It is certainly possible to pass the user's id along from request to request, but that's not typical for most apps.

For an example you can take a look at Devise authentication framework, which provides you a "current_user" method that is accessible from any controller:

Hi everyone! I have this form, “users/add_photo.html.erb” This form is render with the instance variable @photo. I want to pass the id of the current user. This form is called throught this method in “users_controller.erb” def add_photo @photo = Photo.new @us_id = params[:id] render ‘add_photo’ end

The thing is that I’m not available to send the current id of the user, the form is not recongnizing @us_id. Any idea?.

<%= form_for @photo, :url => photos_path, :html => { :multipart => true } do |f| %>

<div class="field">
  <%= f.label :title %><br>
  <%= f.text_field :title %>
</div>
<div>
    <div>
      <%= hidden_field(:id, :us_id)
    </div>
      <%= f.file_field :avatar %>
</div>
<div class="actions">
  <%= f.submit %>
</div>

<% end %

Thank you very much. Regards.

Alfredo

Hi everyone! I have this form, "users/add_photo.html.erb" This form is render with the instance variable @photo. I want to pass the id of the current user. This form is called throught this method in "users_controller.erb"

def add_photo     @photo = Photo.new     @us_id = params[:id]     render 'add_photo'   end

The thing is that I'm not available to send the current id of the user, the form is not recongnizing @us_id. Any idea?.

What do you mean by 'the form is not recognising @us_id'? Is it showing an error, if so what error?

Colin

Ok I think there is another easy way. What I’m trying to do now is send the id of the user that is trying to upload a new photo to the form “new_photo”. That form has the id of the user but when the “submit” is selected the id get lost.

The other way to get the user_id is with the “set_user”, but I don’t know exactly how it works. I’m reading a few blogs and when the user login to the web the application has to save the user_id. Then, any controller can has that id.

Could you please explain to me this way?.

Thank you & Best regards,

Alfredo.

Ok I think there is another easy way. What I'm trying to do now is send the id of the user that is trying to upload a new photo to the form "new_photo". That form has the id of the user but when the "submit" is selected the id get lost.

The other way to get the user_id is with the "set_user", but I don't know exactly how it works. I'm reading a few blogs and when the user login to the web the application has to save the user_id. Then, any controller can has that id.

Could you please explain to me this way?.

What gem (if any) are you using to provide user login? Most have a current_user method or similar that will give you the current user so you don't need to pass it around at all.

Colin