views:

110

answers:

3

Hello! I'm trying to have an image that when clicked associates the selected guideline to a project. I'm using link_to_function which somewhat behaves but I can not get the method I am calling in the link_to_function to redirect to another page. Is there a better way to do this? Below is a bit of my code. I can paste in additional parts if necessary:

<% @guidelines.each do |guideline| %>
 <tr>
    <td align='center'><%= link_to_function image_tag("../../../images/icons/action_add.png"), add_guideline(guideline)  %></td>
          <td><%=h guideline.title %></td>

My GuidelinesController.helper method looks like this:

  def add_guideline(guideline)
           @project = Project.find(params[:project_id])
           @project.guidelines << guideline
           @project.save   

           redirect_to dashboard_path #doesn't work :(
    end
+1  A: 

The Problem

link_to_function is for adding Javascript to your links.

in your code, add_guideline is being called and its result is being passed as the second argument to your link_to_function call.


EDIT

Checkout how to Add more RESTful actions

You can add this to config/routes.rb

# overwrite map.resources :projects
map.resources :projects, :member => {
  :add_guideline    => :get,
  :create_guideline => :post
}

This will add the following routes

   add_guideline_project GET    /projects/:id/add_guideline(.:format)
create_guideline_project POST   /projects/:id/create_guideline(.:format)

In your view, you can use

<%= link_to(
  image_tag("/images/icons/action_add.png"),
  add_guideline_project_path(@project)
) %>

This will link to ProjectsController#add_guideline.

Here you can do what you've been trying:

# app/controllers/projects_controller.rb
def add_guideline
  @project = Project.find(params[:id])

  # render app/views/projects/add_guideline.html.erb
  # create your form in this view
end

def create_guideline
  @project = Project.find(params[:project_id])
  @guideline = Guideline.find(params[:guideline_id])
  @project.guidelines << @guideline
  redirect_to dashboard_path
end
macek
I tried that previously. What happens then is that if I click on the link the url turns from:http://localhost:3000/projects/3/guidelinesto http://localhost:3000/projects/3/guidelines#The guideline is also not added to the project :(
Jeff
actually what I get is:No route matches "/projects//guidelines/new" with {:method=>:get}
Jeff
If I specify the id I am able to redirect. However it goes to guidelines/new where I can create a new guideline, which is not what I want to do. I want to add an existing guideline to an existing project.
Jeff
@Jeff, I updated the `link_to` line in my post. Since you didn't paste them, I don't know what variables you have in your view, so I was sort of expecting you to cover your bases on that. We're you expecting to do this with Ajax or something?
macek
I suppose so. The flow is basically this:Navigate to projects/3.click button to Add Existing Guideline.See list of existing guidelines on a new page.Click add button to add one of the guidelines to project/3.Redirect back to project/3.
Jeff
I made edits to my answer based on your new details.
macek
excellent. I see where this is going. One last problem I am having is:ActiveRecord::RecordNotFound in ProjectsController#add_guidelineCouldn't find Project without an IDI must be missing one little piece :\
Jeff
This is what the request parameters look like:RequestParameters:{"id"=>"3"}
Jeff
oh, use `params[:id]` instead of `params[:project_id]` then.
macek
I see. This still essentially requires me to render the same view twice. I'm assuming this is because link_to is linking to add_guideline_project which is then rendering add_guideline.erb. What I actually want is the link to automatically add the chosen guideline to the project and redirect back to the project. Basically I want something that lets me use a method like add_guideline_to_project(guideline) .. so I pass in the guideline to the method ... the method grabs the guideline and assigns it to the project's guidelines, saves the project, and then redirects to @project.
Jeff
Jeff, you can skip the `add_guildeline` action and just have your form `post` to `create_guideline_project` with `params[:project_id]` and `params[:guideline_id]`
macek
A: 

The helper is evaluated and result returned before the page is rendered... link_to_function uses JavaScript client-side after the page has been rendered.

Guessing from your code, you should probably have an action in your controller that take project_id and guideline_id to add a guideline to a project. Your link should then reflect this such as projects_add_guideline_path(project, guideline) with the contents of your helper in the new action.

In view:

link_to projects_add_guideline_path(@project, guideline)

In controller:

def add_guideline_to_project
  @project = Project.find(params[:project_id])
  @guideline = Guideline.find(params[:guideline_id])
  @project.guidelines << @guideline
  @project.save   
  redirect_to dashboard_path 
end
Reuben Mallaby
Where does projects_add_guideline_path come from?
Jeff
in other words:undefined method `projects_add_guideline_path' for #<ActionView::Base:0x1037508d8>
Jeff
Sorry, it was an example...you'll need to define a route in config/routes.rb
Reuben Mallaby
A: 

I got this working by using

<%= link_to(image_tag("/images/icons/action_add.png"), add_guideline_to_project_path(@project, :guideline_id => guideline.id))  %></td>
    <td><%=h guideline.title %></td>

and this method:

  def add_guideline_to
    @project = Project.find(params[:id])
    @guideline = Guideline.find(params[:guideline_id])
    @project.guidelines << @guideline
    @project.save

    redirect_to @project
  end

I also updated my routes to include:

map.resources :projects, :member => {
  :add_guideline    => :get,
  :create_guideline => :post
}

Thanks everyone!

Jeff