Adding an item into a basket with link_to


#1

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


#2

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 :wink:


#3

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 :wink: