setting foreign key relation...

12 views
Skip to first unread message

Mark Locklear

unread,
Nov 26, 2012, 4:39:24 PM11/26/12
to ashevi...@googlegroups.com
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

unread,
Nov 26, 2012, 6:08:30 PM11/26/12
to ashevi...@googlegroups.com
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 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.

pmeserve

unread,
Dec 4, 2012, 2:22:53 PM12/4/12
to Asheville Ruby Users Group
Without really understanding what a "registration" is in this context,
what I'd probably do is use a nested route so you're going to:
/articles/1/registrations/new

then a library like inherited_resources with a 'belongs_to :article,
optional: true' in the registrations controller can automatically
scope your models and queries correctly, without any additional code

- Paul

On Nov 26, 6:36 pm, Chris Sessions <csessi...@gmail.com> wrote:
> 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
>

Mark Locklear

unread,
Dec 4, 2012, 2:32:47 PM12/4/12
to ashevi...@googlegroups.com
Thanks PM, I am doing exactly what you describe, although I still have the hidden_field in my view which I bugging me. I know I should be able to 'build' the registration in the controller, but it was not happening for me, so I went with what was working for now.

I am also with you on the scoping of data for this app. I am going to be doing alot of 'filtering' of data, so really want to build the appropriate scopes to do this. In the past I have done alot of filtering in the view, or creating methods in my models like:

def self.get_registrations(current_user)
  current_user.registrations
end

...this feels dirty, and I know there are better ways to do this with scopes, so look for some questions in the future for stuff like this. Thanx!
Reply all
Reply to author
Forward
0 new messages