Push user.id to another Controller

27 views
Skip to first unread message

Alfredo Barrero

unread,
Apr 15, 2014, 5:12:11 AM4/15/14
to rubyonra...@googlegroups.com
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

*** Mateus ***

unread,
Apr 15, 2014, 11:46:56 AM4/15/14
to rubyonra...@googlegroups.com
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b6ffe45d-de31-47d2-8609-f3c08eaa15ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Benjamin Iandavid Rodriguez

unread,
Apr 15, 2014, 12:51:42 PM4/15/14
to rubyonra...@googlegroups.com

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

--

Robert Walker

unread,
Apr 16, 2014, 2:20:21 AM4/16/14
to rubyonra...@googlegroups.com
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:

https://github.com/plataformatec/devise

--
Posted via http://www.ruby-forum.com/.

Alfredo Barrero

unread,
Apr 21, 2014, 2:37:15 PM4/21/14
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Apr 21, 2014, 4:41:02 PM4/21/14
to rubyonra...@googlegroups.com
On 21 April 2014 19:37, Alfredo Barrero <abarr...@gmail.com> wrote:
> 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

>
> <%= 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
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/92914078-53bd-4a95-b7bf-010619c97c67%40googlegroups.com.

Alfredo Barrero

unread,
Apr 21, 2014, 4:46:56 PM4/21/14
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Apr 21, 2014, 5:03:12 PM4/21/14
to rubyonra...@googlegroups.com
On 21 April 2014 21:46, Alfredo Barrero <abarr...@gmail.com> wrote:
> 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

>
> Thank you & Best regards,
>
> Alfredo.
>
> El martes, 15 de abril de 2014 11:12:11 UTC+2, Alfredo Barrero escribió:
>>
>> 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
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/ddd70b5b-b3b1-480e-a4bd-235b68a352a8%40googlegroups.com.

abarrero90

unread,
Apr 21, 2014, 5:14:31 PM4/21/14
to rubyonra...@googlegroups.com
I'm not using any gem, I did by myself the login method. Could you tell me any gem?

Thanks.
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/3K-14GwoqhE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Apr 21, 2014, 5:21:14 PM4/21/14
to rubyonra...@googlegroups.com
On 21 April 2014 22:14, abarrero90 <abarr...@gmail.com> wrote:
> I'm not using any gem, I did by myself the login method. Could you tell me
> any gem?

Read the reply by Robert Walker in this thread on 16th April. Also
google can be very helpful. You are more likely to get help here if
you give the impression of having tried to answer questions yourself
before asking us to spend our time trying to help.

Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/xoycrdfo77ip1av6j2dx2e1n.1398114871836%40email.android.com.

abarrero90

unread,
Apr 21, 2014, 5:30:24 PM4/21/14
to rubyonra...@googlegroups.com
Ok, thanks but there is not need to answer like that.
Reply all
Reply to author
Forward
0 new messages