0

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

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

10 Comments

I tried that previously. What happens then is that if I click on the link the url turns from: localhost:3000/projects/3/guidelines to localhost:3000/projects/3/guidelines# The guideline is also not added to the project :(
actually what I get is: No route matches "/projects//guidelines/new" with {:method=>:get}
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, 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?
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.
|
0

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

3 Comments

Where does projects_add_guideline_path come from?
in other words: undefined method `projects_add_guideline_path' for #<ActionView::Base:0x1037508d8>
Sorry, it was an example...you'll need to define a route in config/routes.rb
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.