Re: [Asheville.rb] Digest for asheville-rb@googlegroups.com - 2 Messages in 1 Topic

6 views
Skip to first unread message

Benjamin Lieb

unread,
Nov 27, 2012, 2:03:13 AM11/27/12
to ashevi...@googlegroups.com
Mark, 

I agree with Chris that you'll need to set a hidden form field to persist that info, or use a session. It sounds like the article is an association of registration? That sounds a bit strange to me, but if that is the case you'll probably want this:

def new
  @registration = Registration.new
  @registration.article = Article.find params[:article_id]
end

And then probably use fields_for :article, and then a hidden :id field. 

If it's not an association, you can just send it outside of the :registration has that the form sends.


On Nov 27, 2012, at 1:54 AM, ashevi...@googlegroups.com wrote:

Group: http://groups.google.com/group/asheville-rb/topics

    Mark Locklear <marklo...@gmail.com> Nov 26 04:39PM -0500  

    Hey guys, I have a parameter that I am setting with a 'link_to' in the view
    that looks like this...
     
    <%= link_to 'Register', new_registration_path({:article_id => @article})
    %>
     
    ...and this correctly takes me to...
     
    http://localhost:3000/registrations/new?article_id=1
     
    Now my question is how do I get this into my parameters when I submit this
    page?
     
    In my registrations controller I can do...
     
    def new
    @registration = Registration.new
    @article = Article.find params[:article_id]
    ...
    end
     
    ...and if inspect @article it is correct...
     
    Now I'm just now sure how to get that :article_id parameter over to the
    create action.
     
     
    --
    J. Mark Locklear
    -Philippians 4:13 gives you the muscle, but YOU have to flex it!
     
    "One machine can do the work of fifty ordinary persons. No machine can do
    the work of one extraordinary individual."
    -- Elbert Hubbard
     
    Chris Sessions <cses...@gmail.com> Nov 26 06:08PM -0500  

    Hey Mark,
     
    Your new action would populate an article_id hidden field tag if you include one in your form, and that would be submitted with the rest of the registration form.
     
    You might alternately look at keeping the article_id behind the scenes as a session variable if there's any liability to passing it out in the open.
     
    Hope that helps.
     
    - Chris
     
     
     
     
     
     

You received this message because you are subscribed to the Google Group asheville-rb.
You can post via email.
To unsubscribe from this group, send an empty message.
For more options, visit this group.


--
You received this message because you are subscribed to the Google Groups "Asheville Ruby Users Group" group.
To post to this group, send email to ashevi...@googlegroups.com.
To unsubscribe from this group, send email to asheville-rb...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asheville-rb?hl=en.

Mark Locklear

unread,
Nov 27, 2012, 8:30:24 AM11/27/12
to ashevi...@googlegroups.com
Thanx for the extra set of eyes guys. I swear I tried using a hidden field yesterday, and it just wasn't happening, but I just implemented it as ya'll suggested, and it works!

Andy Vanasse

unread,
Nov 27, 2012, 9:43:30 AM11/27/12
to Asheville Ruby Users Group
Is there a compelling reason to avoid using a nested route for this?

# @article = Article.find 1
# new_article_registration_path(@article)
=> articles/1/registrations/new

That the article id is always part of the params when you need it.  If this path is not a typical access, you could restrict the available routes in the map to only those few that you need:

resources :articles do 
  resources :registrations, :only => [:new, :create]
end


On Tue, Nov 27, 2012 at 2:03 AM, Benjamin Lieb <ben...@pixelearth.net> wrote:

Mark Locklear

unread,
Nov 27, 2012, 10:31:55 AM11/27/12
to ashevi...@googlegroups.com
Good call Andy I like this better. It did not occur to me to use a route to accomplish this.

Is there any advantage to using...

    <%= link_to 'Register', new_article_registration_path(@article) %>

over...


    <%= link_to 'Register', new_registration_path({:article_id => @article}) %>

...other than a cleaner looking route?

Andy Vanasse

unread,
Nov 27, 2012, 11:07:50 AM11/27/12
to Asheville Ruby Users Group
At the pragmatic level, things like hidden fields and session variables always feel "dirty" to me.  They're acceptable but I try to avoid them.

At a philosophical level, there is a semantic difference between the two.  With the nested route you convey the idea that you are registering for an article.  With the query parameter approach you convey the idea that you are creating a registration that happens to include article information.  The first approach says that the article is the focus of the action while the latter says that it's incidental.

Mark Locklear

unread,
Nov 27, 2012, 11:40:57 AM11/27/12
to ashevi...@googlegroups.com
Hmm...actually I still have the hidden_field in place, and I fail to get the article_id parameter without it. Do I need something additional in my registrations controller perhaps?

Andy Vanasse

unread,
Nov 27, 2012, 12:29:34 PM11/27/12
to Asheville Ruby Users Group
If you're using the nested route then your new and create should contain something like this:
@article = Article.find params[:article_id]
@registration = @article.registrations.build params[:registration]
...

It's possible that having the hidden field might override the article_id assignment that's automatically made via the association and without code like the above you might be overriding with nil.  I'm not sure when ARec makes the assignment from the association parent.  I'd remove the hidden field if you're using nested routes.

If you're taking the id-as-param alternative then make sure the above two lines appear in your 'new' action.  That will make sure that the article_id is present in the registration instance used to build the form and will populate the hidden article_id field.
Reply all
Reply to author
Forward
0 new messages