Better solution than accessing routes in the Model? - ruby-on-rails

So I know that you shouldn't use UrlWriter methods in a model, but I wasn't sure what a better way to handle my current situation is.
Basically, I want to allow a User to post something to his facebook feed, and want to write a post_to_fb_feed(object) method in class User. Now, the URL of what is actually posted depends on the object, so I also have a to_fb_feed_item method on the object classes that a user can post. The to_fb_feed_item method just returns a hash that the Facebook API expects, including the url the post should link to.
I've gotten this to work currently, by including ActionController:UrlWriter in my models, but I was wondering if anybody had a better suggestion for how to handle this.
Thanks!
Eric

including ActionController:UrlWriter is the best way to handle it. I don't know why it's not easier to generate urls from arbitrary places in Rails -- sure it might be more common for the appropriate place to be in controllers and views, but the fact of the matter is many models validly deal with urls as data, and need to generate them.

Related

Best method for simple GET based Ruby on Rails app

Im trying to understand the best method for creating a basic Rails app that is only interfaced with via GET values in the url. Im attempting to understand how to go about starting to write logic for this as well as generally where i would want to start coding. Would this main interface code be considered a helper? or should i migrate the code to the model?
An example of how i'd like to use this app is something like the following.
http://www.railsapp.com/?order_number=123&print_label=true
With this example url i hope to grab the order_number get some information from it, determine if print_label is true/false and output something to the user.
Im not looking for someone to write it all for me, i'm simply needing some guidance on the best structure on writing a rails app that is only interfaced with via a URL as well as the easiest method for gather GET values.
Between the 3 comments on the first post i am going to looking into Restful routing and by that gather my params to interact with them.

How can I omit controllers for two models in rails 3 routes?

I'm building a rails3 application and at the moment I have the following line in my routes.rb file:
get "/:id" => 'tapes#show'
In other words, you can show a Tape using website.com/tapes/1 and also by using website.com/1
(I am also using friendly_id gem so the user sees in fact a friendly URL in the form of website.com/tapename)
Now, what I would like to achieve is to do the same thing for Users pages. So instead of showing a User page using website.com/users/alex I want to be able to also use website.com/alex.
Is there a way to implement this 'users' logic in routes.rb together with the existing 'tapes' rule and somehow set a priority?
So if someone accesses website.com/alex my app would search if there is a User record with id 'alex' and if none is found then look for a Tape with id 'alex'.
Could I use some kind of Advanced Constraints in routes?
Any ideas?
Many thanks for the help,
Alex
Rails would have no way to determine which controller you were trying to access. The only way that this would be possible, is if either:
you could determine which model it would resolve to based upon some regular expression on the name.
or
You knew that user names and tape names never conflicted, and were willing to suffer the cost of hitting the database to resolve the correct controller.
This is probably a bad idea for a number of reasons, it would have performance implications, it also doesn't conform to RESTful principles, and it would probably be confusing to users.

What is the cleanest way to create an instance of a model that belongs_to <parent_model>?

I am pretty sure there is and answer out there.. But I can't seem to find what is the cleanest way to do it, and I am just starting playing around with Ruby and Rails 3.1.
I have a Client model and a Project model.
I'd like to have a button on the client#show view that leads to a project#new where there is no need to input the client_id.
Furthermore the project#new should still be accessible on his own and ask for the client_id if it is not available.
Any help appreciated!
Thanks
Take a look at nested resources:
http://guides.rubyonrails.org/routing.html#nested-resources
Using that take care of the passing through of the parent ID. You can have a route that goes directly to project#new as well and then you'll just need to handle whether to ask for the client ID in the view depending on whether it is already defined.

RESTful Actions Best Practices

I've got a RESTful User model working well in Rails 3. I'd like to add a new option to create a new user based on information queried out of a LDAP server.
What I'd like advice on is how best to do this. Here's what I've thought up so far, but I don't know if it matches Rails best practices:
Edit the resource path of User to accept both GET and POST to a new view called "import_ldap_user".
Import LDAP user then presents a form which uses AJAX (POSTing to import_ldap_user) to allow the visitor to search for a person in LDAP. The results are displayed on the page and if acceptable, the user clicks "Create", which then calls /user/create.
Part of why this seems bad to me is:
I need to post a proper #user to /user/create, but I'm not sure if my AJAX call can produce a proper #user.
I don't know if it's a bad practice to add a new verb to the RESTful Users route.
I don't know if using an AJAX POST to import_ldap_users is a proper separation of concerns.
Any ideas? Any Rails perfectionists have opinions about how this should work?
What gets posted to /user/create isn't an #user object but rather its attributes. A scaffolded create action will probably have something akin to #user.new(params[:user]), which just pulls the user attributes that were posted and creates a new object based on that.
Even if your AJAX call doesn't provide the attributes in a manner that can be processed by the new method, you can simply modify your create such that it manipulates the post data.
As for best practices, this is definitely something I've thought about in the past but I don't know if there's a "correct" answer. I think having a new view which posts to the create method is perfectly acceptable, you could also create a new controller if you want to strictly follow the CRUD pattern.
Definitely a good question and if anyone has a better answer I'd love to hear it.

Ruby on Rails - Create Entity with Relationship

I'm new to rails, so be nice.
I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time.
For (a contrived) example:
My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for Person it makes sense for the user of my appliction to create the person, and the contact_info at the same time.
It doesn't seem right to include details for creating a contact directly in the create view/controller for person. What's the rails way to handle this?
Using nested attributes is the most common way to do this.
The actual documentation is here.
You want to use "Nested Forms". There is a great example of them in this blog post.
I'm also noob, but I had a similar issue with an app. I was using a tutor at the time and he basically said it was a good example of rails being opinionated. It sounds like you want to take the create action for two different models at the same time, which may be possible but probably very hard. Id suggest considering whether your data model could be modified, or find a way to make an acceptable user flow while collecting the data in different forms.
Update: while writing this the technical answer came in. Keep in mind, its perfectly okay to take the easy route if doing so helps you get the app out the door, and especially while you're still new.

Resources