# Adding an item into a basket with link\_to

**URL:** <https://www.ruby-forum.com/t/adding-an-item-into-a-basket-with-link-to/153086>\
**Category:** Rails\
**Created:** [November 12, 2008, 12:00am UTC](https://www.ruby-forum.com/t/adding-an-item-into-a-basket-with-link-to/153086 "2008-11-12T00:00:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 12, 2008, 12:00am UTC](https://www.ruby-forum.com/t/adding-an-item-into-a-basket-with-link-to/153086/1 "2008-11-12T00:00:14Z")

</div>

Hi there,

well, I’m trying to follow an idea I’ve seen somewhere else but can’t  
get it customized to my needs.

Situation: I have a plant model, a user model and a garden model.

Plant model has\_many :gardens  
User model has\_many :gardens  
Garden model belongs\_to :user and belongs\_to :plant

I’ve choosen garden being a rich joined table because I want additional  
information in it, like notice, etc.

Everybody might be able to see the list of plants by the index view and  
detailed information in the show view. I wanted the system to be gently,  
so if the user is not logged in he gets an information “become a member,  
add the plant to your garden, etc.” As the user logs in, he might go as  
well through the index and follow that up to the show action but then  
I’m checking if the plant already exists in the garden or not. This is  
releasing different actions.

plants\_controller:  
def show  
@plant = Plant.find\_by\_id\_and\_published(params[:id], true)  
@title = @plant.name\_deu  
if is\_logged\_in?  
@plant\_in\_garden = Garden.find\_by\_user\_id(params[:id].to\_i)  
else  
@plant\_in\_garden = nil  
end

show of plant:

# \<%= h(@plant.name\_deu) %\> \<% if is\_logged\_in? && @plant\_in\_garden || nil %\> is already in your garden. \<% end %\>

\<% if is\_logged\_in? && @plant\_in\_garden == nil %\>

I don't know which code to write here ;)

\<% end %\>

This all works well right now. I put some data in the gardens table.  
Logged in and logged out, with and without plant in garden great.

Now, how to get the plant into the garden with one click??? Now we get  
back to the idea I’ve seen.

routes.rb:  
map.resources :plants,  
:member =\> { :enable =\> :put, :disable =\> :delete },  
:collection =\> {:admin =\> :get}

One guy did it with the posibility as an admin for enabling and  
disabling members:

\<% unless user == logged\_in\_user -%\>  
\<% if user.enabled -%\>  
\<%= link\_to image\_tag(“key\_delete.png”), user\_url(user.id), :method  
=\> :delete %\>  
\<% else -%\>  
\<%= link\_to image\_tag(“key\_add.png”), enable\_user\_url(user.id),  
:method =\> :put %\>  
\<% end -%\>  
\<% end -%\>

and in the controller:  
def destroy  
@user = User.find(params[:id])  
if @user.update\_attribute(:enabled, false)  
flash[:notice] = “User has been disabled.”  
else  
flash[:error] = “User couldn’t be disabled.”  
end  
redirect\_to :action =\> ‘admin’  
end

def enable  
@user = User.find(params[:id])  
if @user.update\_attribute(:enabled, true)  
flash[:notice] = “User has been enabled.”  
else  
flash[:error] = “User couldn’t be enabled.”  
end  
redirect\_to :action =\> ‘admin’  
end

So, how to get that running for my needs? The above code isn’t using a  
joined table. It is writing in the user model itself an setting the  
enabled flag. I need it in an joined table.

Am I wrong, am I right? How to get where I want to be? Any help?  
Greetings,  
Mayo

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 12, 2008, 12:34am UTC](https://www.ruby-forum.com/t/adding-an-item-into-a-basket-with-link-to/153086/2 "2008-11-12T00:34:18Z")

</div>

I know there is a mistake in

plants\_controller:  
def show  
@plant = Plant.find\_by\_id\_and\_published(params[:id], true)  
@title = @plant.name\_deu  
if is\_logged\_in?  
@plant\_in\_garden = Garden.find\_by\_user\_id(params[:id].to\_i)  
else  
@plant\_in\_garden = nil  
end

I’m just looking for if there is this user in it, I’ll change that but  
that doesn’t solve the big challenge 😉

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 12, 2008, 12:57am UTC](https://www.ruby-forum.com/t/adding-an-item-into-a-basket-with-link-to/153086/3 "2008-11-12T00:57:41Z")

</div>

I changed the show action:

def show  
@plant = Plant.find\_by\_id\_and\_published(params[:id], true)  
@title = @plant.name\_deu  
if is\_logged\_in?  
@plant\_in\_garden = Garden.find\_by\_plant\_id(params[:id].to\_i,  
:conditions =\> { :user\_id =\> logged\_in\_user.id})  
else  
@plant\_in\_garden = nil  
end

That works fine. I would have changed that in the first post, it’s just  
not possible to edit in that board. I hope you still see my problem 😉
