nested ressources, still requires unnested base ressource in rails3.1 - ruby-on-rails-3.1

I seem to require in Rails 3.1 for a nested resources both, instead of having photo just nested:
resources :photos
resources :gallery do
resources :photos
end
Otherwise my form_for will not work for photos to submit or galleries to view:
<%= form_for ([#gallery, #photo],:html => {:multipart => true}) do|f| %>
Complaining on show of gallery:
No route matches {:id=>#<Photo id: 23 ...
Is that normal behavior, I always want my users to only be able to create photos in the context of a gallery, but now they can also access photos/new while I would only let them access gallery/:id/photos/new

Routes can be tricky. Sometimes using the plural instead of the singular form causes an error, especially for path helpers. The plural of "Gallery" is "Galleries"
>> "Gallery".pluralize
=> "Galleries"
Check your routes with rake routes, I think for your purpose you should use the plural form
resources :galleries do
resources :photos
end

Related

Create different sign up pages for users with different roles using Devise in Ruby on Rails

I have 2 different sign up pages one for normal user and one for fundraiser. for creating user I am using Devise and have normal fields as Name, email and password.
For fundraiser these fields are common with some extra fields so for that I am using this in user.rb
attr_accessible :campaign_fundraisers_attributes
has_one :campaign_fundraisers
accepts_nested_attributes_for :campaign_fundraisers, allow_destroy: true
this in CampaignFundraiser.rb
belongs_to :user
and this in my create_fundraiser.html.erb
<%= form_for(resource, :as => resource_name, :class => 'reg',:url => registration_path(resource_name)) do |f| %>
<% resource.build_campaign_fundraisers if resource.campaign_fundraisers.nil? %>
<%= f.fields_for :campaign_fundraisers do |u| %>
here I am able to create user and save data in both the user's and fundraiser's table on form submission but the problem i am facing here is that whenever I get an error (by devise validation) i am getting redirected to the sign-up page for normal user with error messages being displayed there.
I want a solution so that I get the error message on the same create fundraiser page without being redirected on the normal user's sign up page.
So please suggest any solution for my problem considering I am new to Rails and this is my first post here.
You can use config.scoped_views like in this documentation said, config.scoped_views
config/initializers/devise.rb
config.scoped_views = true
then, you can use views based on the role like users/sessions/new or students/sessions/new. If no view is found it will use the defaults at devise/sessions/new

How to insert image into SQLite with Sequel and Sinatra?

I'm trying to insert images to each one of my blog posts using SQLite with Sequel and Sinatra.
DB.create_table :posts do
primary_key :id
String :title
String :content
Datetime :created_at
Datetime :updated_at
foreign_key(:user_id, :users, :type=>String)
end
What changes should I make to my db for that and how can insert the image from the view and later display it?
Any help or idea is highly appreciated.
Personally, if it's for a blog then I'd upload the file somewhere - whether that be a filesharing site (your Dropbox or something), and then write an img link to it in the article's text. Why complicate things?
Edit: I'm not suggesting embedding HTML in your document, but (such as in Markdown) an indicator that is then further processed into HTML later, much as the rest of the document will.
I'd also rename Post to Article, since POST is an HTTP verb and used in Sinatra, or else you end up with stuff like this:
post "/post" do
post = Post.new title: "First Post"

rails 3 show from related tables

In my events_controller, if I use the following:
def index
respond_with(#market.events) do |format|
format.js {render :json => #market.events, :callback => params[:callback]}
end
end
I get the expected response. Events is a nested resource under markets.
But I need to also return the asset associated with the event, which is in a related table. If I try the following:
respond_with(#market.events.joins #market.events.assets) do |format|
I get undefined method `assets' for #ActiveRecord::Relation:0x1088215a0. On my events show page, I can do asset.asset.url and it shows.
Any ideas on where I've gone wrong?
There are multiple events, and you're trying to call assets on the collection of events—that is, a set of many events, which doesn't have assets, though each element in the collection has assets.
You say you want to get the asset (singular) for the event (also singular), so I'm not really sure what you actually want to achieve here, since you're returning multiple assets. To get all the assets for all the events you can do:
#market.events.map(&:assets).flatten # If event has many assets
#market.events.map(&:asset) # If event has one asset
Have you tried eager loading ? With #market.events.includes(:assets), you should have the assets too.

Polymorphic urls with singular resources

I'm getting strange output when using the following routing setup:
resources :warranty_types do
resources :decisions
end
resource :warranty_review, :only => [] do
resources :decisions
end
I have many warranty_types but only one warranty_review (thus the singular route declaration). The decisions are polymorphically associated with both. I have just a single decisions controller and a single _form.html.haml partial to render the form for a decision.
This is the view code:
= simple_form_for #decision, :url => [#decision_tree_owner, #decision.becomes(Decision)] do |form|
The warranty_type url looks like this (for a new decision):
/warranty_types/2/decisions
whereas the warranty_review url looks like this:
/admin/warranty_review/decisions.1
I think because the warranty_review id has no where to go, it's just getting appended to the end as an extension.
Can someone explain what's going on here and how I might be able to fix it?
I can work around it by trying to detect for a warranty_review class and substituting #decision_tree_owner with :warranty_review and this generates the correct url, but this is messy. I would have thought that the routing would be smart enough to realise that warranty_review is a singular resource and thus discard the id from the URL.
This is Rails 3 by the way :)
Apparently it's a long standing rails bug where polymorphic_url has no way of knowing whether a resource is singular or not from the routes setup:
https://rails.lighthouseapp.com/projects/8994/tickets/4077-wrong-redirect-after-creation-of-nested-singleton-resource-using-responder
I'm just going to resort to using a non-singular route even though there will only ever be one warranty_review. It's just aesthetics at the end of the day.

Route question regarding Authlogic and Rails 3.0.0

I upgraded an app I am working on from Rails 3.0.0.beta4 to Rails 3.0.0 and hit an unexpected error. I'm using authlogic for authentication, and after the upgrade the route for new user session form started throwing this error.
undefined method `user_sessions_path'
Ok, I'm using a singular controller name. Not sure what is different between beta4 and the new release that caused the problem.
In my routes.rb file I have this:
get "user_session/new", :as => :login
get "user_session/destroy", :as => :logout
resources :user_session, :controller => :user_session
Which defines my singular controller routes.
To fix the problem I had to change the first line of the form from this:
<%= form_for #user_session do |f| %>
to this:
<%= form_for #user_session, :url => user_session_index_path do |f| %>
What is striking me as weird is the name of the route. Running rake routes revealed the name of the route, but I don't understand why the index was needed. I was expecting something more like user_session_path for the post method. My user_session routes are the only ones acting this way. All of the other are as I expect.
Heres the output from rake routes:
user_session_index GET /user_session(.:format) {:action=>"index", :controller=>"user_session"}
user_session_index POST /user_session(.:format) {:action=>"create", :controller=>"user_session"}
This works, but I'm curious to know if anyone else has encountered this.
First of all, I don't understand, why do you specify the controller name, when it is the same as the resources name? Those two, are equivalent:
resources :user_session, :controller => :user_session
resources :user_session
AFAIR by design create just does POST onto the same path as index. By definition, when controller name is singular, router won't be able to create a plural version of the name. It works the other way around. Thus, theres a _index suffix in both index and create actions.
Singular name would suggest, that the resource is singular, so you should just use (sigular for resource):
resource :user_session
In this case there will be no index action (as it makes no sence for singular resource), and the name for create path will be user_session_path.
As a side note, singular resource doesn't imply, that there is only one instance of the model. It just specifies how do you access the resource. For example you can have multiple users with profiles, but there is perfect sense to use singular resource in order to manage the profiles, since each use can only access his own profile.
ps. I moved on to Devise for authentication before Rails 3 and I don't quite remember how routes used to be solved in Authlogic.

Resources