Rails if referrer then do issue - ruby-on-rails-3

I am trying to figure out the best way to build an if else if statement in the controller around a rails specific referrer. Is there a way to pull the last rails path and use it in the below statement? I think I am close but totally stumped...
This is an update action that will be hit from one form at multiple locations on the site.
I am looking to replace "form_path"
def update
#object = Milestone.find(params[:id])
if #milestone.update_attributes(params[:milestone])
if request.referer == form_path
redirect_to root_path
else
redirect_to object2_path
end
else
....
end
end

Is form_path a variable you're defining somewhere else in the controller? Outside of understanding that, it looks like it should work.
Instead of messing with referer, you could place a hidden_field in the form based on where it's coming from, and pull that out of the params hash.
Something like:
hidden_field_tag :location, controller_name
Then in the controller:
if params[:location] == yadda yadda

Related

How do I update a record via params?

I want to be able to update a record via params. Something like this:
http://domain.com/api/v1/notes/2190/notify?message=test
But it just returns a page not found. Pretty standard routes going on:
resources :notes, only: [:show] do
post 'notify'
end
My notify method looks like this:
def notify
#note = Note.find(params[:id])
if params[:message]
render text: #note.update_attributes(message: params[:message])
end
end
Do I need to do anything else to permit this functionality? Any advice at all? I can't figure it out. Cheers.
The routes should be something like:
resources :notes, only: :show do
member do
get 'notify'
end
end

Rails 3 and update_attributes

I am moving an application from Rails 2.2.2 to Rails 3. I have a form that is used to update a user's info that was fully functional in Rails 2 but breaks in Rails 3
When the form is submitted, a method is called that creates the user object and then tries to do the update like this:
if #user.update_attributes params[:user] ## line 126
Then the controller throws this exception:
undefined method `update_attributes' for #<ActiveRecord::Relation:0xacfc178>
in: app/controllers/admin_controller.rb:126:in `save_user'
So it looks like ActiveRecord in Rails 3 is returning a different type of object? One that doesn't inherit update_attributes? How do I fix this?
Here is the full controller method in question:
def save_user
#needs_password_gen = "YES"
#user = B2bUser.where("id = ?",params[:id])
#needsAPICredentials = false
##### Make sure thay gave us an email address
if !params[:user][:email] || !validEmailAddress(params[:user][:email].to_s)
flash[:warning] = "Valid email address is required."
redirect_to :controller => "admin/edit_user/#{#user.id}" and return
end
#user.first.update_attributes params[:user]
end
THANKS
Looks like your #user object may be an array. You are probably using :where to query and are forgetting to pop it off the array. Try this:
#user.first.update_attributes params[:user]

Rails 3.1 multiple controllers for one path

We have two models, areas and stores, which we want to run off the same path: www.mysite.com/the_name_of_the_thing_here
What we would like to do is go through the areas table for a match to show the area page and, if there is no match, to go through the stores table and show the store page instead. We're not quite sure where to put this logic (in the areas controller?) and how to switch controllers. Any ideas?
Thanks
I think you can use controller action for that, something like
#area = Area.find_by_name(params[:name])
#store = Store.find_by_name(params[:name])
if #area
redirect_to area_path(#area)
elsif #store
redirect_to store_path(#store)
else
redirect_to help_url
end
If you want to change content only make other controller method in which you define variable:
#thing = Area.find_by_name(params[:name]) || Store.find_by_name(params[:name])
and pass it to view
<%= thing.name %>

REST Routes and overriding :id with to_param

My controller is using the default RESTful routes for creating, adding, editing etc
I want to change the default :id to use :guuid. So what I did was:
# routes.rb
resources :posts
# Post Model
class Post < ActiveRecord::Base
def to_param # overridden
guuid
end
end
This works but my modifed REST controller code has something like this
def show
#post = Post.find_by_guuid(params[:id])
#title = "Review"
respond_to do |format|
format.html # show.html.erb
end
end
When I see this this code ..
Post.find_by_guuid(params[:id])
it would seem wrong but it works.
I don't understand why I can't write it out like this:
Post.find_by_guuid(params[:guuid])
Why do I still have to pass in the params[:id] when I'm not using it?
Looking for feedback on whether my approach is correct or anything else to consider.
Even though it works it doesn't always mean it's right.
Type rake routes in your console, and check the output of the routes. You'll see the fragment ':id' in some of them, that's where the params[:id] comes from. It's a rails convention : when you use resources in your routes, the parameter is named id. I don't know if you can change it (while keeping resources; otherwise you could just go with matching rules), but you shouldn't anyway : even if it seems not very logic, it actually has sense, once your understand how rails routing works.

RoR 3.0.9 - Passing a variable from routes.rb to application_controller.rb or viceversa

I have a multi site project that changes from one to another by changing two variables, one is inside routes.rb and the other in application_controller.rb. Is it possible to pass variables between these files so that I only have to change a parameter to achieve the change required?
On my routes.rb file I use this variable to assign the correct controller the routes it should use. For example:
def showsite
"mysite1"
end
root :to => "#{showsite}#index"
And on application_controller.rb I use the same parameter to get the domain of the site, some layouts it should use and another things. For example:
before_filter :set_defaults
def showsite
"mysite1"
end
def set_defaults
if "#{showsite}" == "mysite1"
#domain = 'mysite1.com'
elsif "#{showsite}" == "mysite2"
#domain = 'mysite2.com'
else
#domain.nil?
end
end
def special_layout
"#{showsite}"
end
Every time I want to show a different version of the project I need to change two variables. I know its not a lot but I have to do it many times a day. Im pretty new on RoR, if there is a better solution please guide me to it. Thanks!
Why not use a Rails Initializer. E.g. the following:
File: `config/initializers/showsite.rb`
with the following content:
MyApp::Application.config.showsite = 'mysite1'
Then you should be able to use:
def showsite
MyApp::Application.config.showsite
end
and similar in your routes file.