0

I'm pretty new to rails and I'd like to set my links for a certain page dynamically. I have a table called "Unfinished" and it has a column called "link" (corrected from "links") I'd like to be able to call the "link" record in the view to set my link_to link path.

I am trying to do this...

<%= link_to @unfinished.link(:p => @post.id)  do %>  FINISH <% end %>

...but that's not working.

my controller says:

def show

@post = Post.find(params[:id])

   @unfinished = Unfinished.where('progress = ?', @post.progress).last

end 

and the controller logic works fine...until I try to put the @unfinished.link into link_to

Edit: Error Message:

wrong number of arguments (1 for 0)

Model

  class Unfinished < ActiveRecord::Base
  end

The type of links are :

step1_path
step2_path
step3_path

I am making a multipage form that you can save partway through. Based on a value in the @post.progress column (like 1, 2, 3) the correct path to complete the post will be provided (step1_path, step2_path etc...)

NothingToSeeHere
  • 2,253
  • 5
  • 25
  • 57
  • what error message do you get? does the the unfinished model look like? you said it has a links column, what does your link method look like on unfinished. post that code please.. – Doon Dec 29 '14 at 02:08
  • I don't understand what you mean by "link method on unfinished"...but I posted everything else. – NothingToSeeHere Dec 29 '14 at 02:17
  • you have an Unfinished model? you said it has a column called links. but you are calling a method called `link` and passing in a hash with :p set to the post.id. Does your Unfinished model not have a method called link, that you defined? If not that is part of the issue since ActiveRecord will be looking for Unfinished#links to access that field (It doesn't use the inflector on columns). – Doon Dec 29 '14 at 02:20
  • It is called "link"...I wrote it wrong at first. It should be looking for Unfinished#link and the column name is "link". – NothingToSeeHere Dec 29 '14 at 02:23

1 Answers1

1

try this.

<%= link_to eval(@unfinished.link.to_s)  do %>  FINISH <% end %>

since the link you want is actually a named route, so you will need to eval it.

but with this you wouldn't be able to be able to pass in the post id, which you will need.

If the route is the same for all records (save for what part you are on based on the progress attribute) do you even need to store it in the database? You could just make the link method return the path (that you would still need to eval).

something like

   def link (post) 
      "step#{self.progress}_path(post.id)"
   end

and then eval the link on the way back. but Not sure if that will work, just thinking out loud...

There are gems that do multi-stage forms perhaps looking into them might help?

Doon
  • 19,719
  • 3
  • 40
  • 44
  • Thank you. I'm going to try to put the "def link" logic in the PostController. How would I write that one in the view? – NothingToSeeHere Dec 29 '14 at 02:36
  • how does Post Relate to Unfinished? and I noticed an error in my link since the progress is related to the post.progress, not unfinished.progress. Would you have a post instance in your view? – Doon Dec 29 '14 at 02:39
  • also check out this answer it might help. http://stackoverflow.com/questions/9596168/how-to-dynamically-call-routes-helper-in-rails – Doon Dec 29 '14 at 02:40
  • If @Post.progress = 1 it joins with Unfinished.progress. Unfinished.link then supplies the link to Step 2 of the posting process. I think I'll have to do the first option...so please don't trouble yourself answering my comment. You've answered my question perfectly. – NothingToSeeHere Dec 29 '14 at 02:43