Hi all,

Just wondering about equality in rails for access to certain features
such as the following.

I have a table for games and all users are able to click on the show
button to display the actual record. On the show page it has the options
to go back or edit. I want to do the following

If the current_user equals the id associated with the game
Then allow the to edit
else
redirect the back to the product show page
end

The only line I am not sure on how to do is to match current user with
the id associated user_id of the game.

Any suggestions?

what I have is the following but no luck:

def edit
if current_user.id = @game.user_id
@game = Game.find(params[:id])
else
format.html { redirect_to root_url }
end
end

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Martin Aceto at Feb 7, 2012 at 2:58 pm

    On Tue, Feb 7, 2012 at 11:39 AM, Christopher Jones wrote:

    Hi all,

    Just wondering about equality in rails for access to certain features
    such as the following.

    I have a table for games and all users are able to click on the show
    button to display the actual record. On the show page it has the options
    to go back or edit. I want to do the following

    If the current_user equals the id associated with the game
    Then allow the to edit
    else
    redirect the back to the product show page
    end

    The only line I am not sure on how to do is to match current user with
    the id associated user_id of the game.

    Any suggestions?

    what I have is the following but no luck:

    def edit
    if current_user.id = @game.user_id
    @game = Game.find(params[:id])
    else
    format.html { redirect_to root_url }
    end
    end

    --
    Hi Christopher,

    remember, = is not equal, the correct is

    if current_user.id == @game.user_id

    and look at this

    https://github.com/ryanb/cancan

    maybe help you.

    --
    Martin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dheeraj Kumar at Feb 7, 2012 at 3:12 pm
    if current_user.id = @game.user_id
    @game = Game.find(params[:id])

    Wait, what?

    @game is initially null. So, you should first fetch the game we're talking about.

    @game = Game.find(params[:id])

    Now, check if the game is indeed a valid game.

    @game = Game.find(params[:id])
    if @game
    else
    format.html { redirect_to root_url }

    end

    Check if the current user is the game's user. Note that as @Martin Aceto pointed out, equal conditional operator is ==

    @game = Game.find(params[:id])
    if @game
    if current_user.id == @game.user_id
    format.html
    else
    format.html { redirect_to root_url }
    end
    else
    format.html { redirect_to root_url }

    end





    Dheeraj Kumar

    On Tuesday 7 February 2012 at 8:27 PM, Martin Aceto wrote:
    On Tue, Feb 7, 2012 at 11:39 AM, Christopher Jones (mailto:lists@ruby-forum.com)> wrote:
    Hi all,

    Just wondering about equality in rails for access to certain features
    such as the following.

    I have a table for games and all users are able to click on the show
    button to display the actual record. On the show page it has the options
    to go back or edit. I want to do the following

    If the current_user equals the id associated with the game
    Then allow the to edit
    else
    redirect the back to the product show page
    end

    The only line I am not sure on how to do is to match current user with
    the id associated user_id of the game.

    Any suggestions?

    what I have is the following but no luck:

    def edit
    if current_user.id (http://current_user.id) = @game.user_id
    @game = Game.find(params[:id])
    else
    format.html { redirect_to root_url }
    end
    end

    --
    Hi Christopher,

    remember, = is not equal, the correct is

    if current_user.id (http://current_user.id) == @game.user_id

    and look at this

    https://github.com/ryanb/cancan

    maybe help you.

    --
    Martin
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com (mailto:rubyonrails-talk@googlegroups.com).
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com (mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Christopher Jones at Feb 7, 2012 at 7:34 pm
    Been working on it this afternoon guys and made a fair few tweaks and it
    has resulted in success.

    Thanks for all the help guys.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dheeraj Kumar at Feb 7, 2012 at 7:37 pm
    You're welcome :)


    Dheeraj Kumar

    On Wednesday 8 February 2012 at 1:04 AM, Christopher Jones wrote:

    Been working on it this afternoon guys and made a fair few tweaks and it
    has resulted in success.

    Thanks for all the help guys.

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com (mailto:rubyonrails-talk@googlegroups.com).
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com (mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 7, '12 at 2:39p
activeFeb 7, '12 at 7:37p
posts5
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase