In Rails 3 how could I create a db only contact form not using scaffold? - ruby-on-rails-3

New to rails 3
I would like to create a contact form that people fill out, its saved to the db and then a thank you page comes up.
I would like to do this without scaffold so I can learn better, and I figure that by doing it this way it would be easer to setup so that people cannot try and look at other people's entries by modifying the url.
ideally it would keep their state in the session or cookie so that they would end up on the thanks page if they came back.
Have been trying to do this for about 3 days and reading/googling tons, but between the new routes redirect_to controller stuff in rails3 havn't managed to figure it out.
Routes.rb
Contact::Application.routes.draw do
resources :contactees, :only => [:new, :create]
# to make sure crud doesn't have the routest I don't want
get 'contactees/submitted'
root :to => 'contactees#new'
contactees_controller.rb
ContacteesController < ApplicationControler
def share
end
def new
#contactee = Contactee.new
end
def create
#contactee = Contactee.new(params[:contactee])
if #contactee.save
redirect_to submitted_contactee
else
render action: "new"
end
end
end
Views
contactees
_form.html.erb
new.html.erb
submitted.html.erb

Get rid of the submitted route, you don't need it. Perhaps something like this?
def new
render :submitted if session[:contacted]
end
def create
#contactee = Contactee.new(params[:contactee])
if #contactee.save
session[:contacted] = true
render :submitted
else
render action: "new"
end
end

Related

Rails goes to show action when I want custom action of open or expired

I am basically trying to do a custom index
In my routes.rb I have:
resources :tokens do
resources :visits
collection do
get :open
get :expired
end
end
My routes show as :
open_tokens GET /tokens/open(.:format) tokens#open
expired_tokens GET /tokens/expired(.:format) tokens#expired
I have open and expired defined in my tokens controller
def open
#q = Token.open_token.includes(:households, :visits).search(params[:q])
#tokens = #q.result
respond_to do |format|
format.html {render template: 'tokens/open'}
end
end
def expired
#q = Token.expired_token.includes(:households, :visits).search(params[:q])
#tokens = #q.result
respond_to do |format|
format.html {render template: 'tokens/expired'}
end
end
But it goes to my show controller with the following error:
Couldn't find Token with id=open
and it crashes at my Show action:
Better Errors shows the request as:
{"action"=>"show", "controller"=>"tokens", "id"=>"open"}
It seems to me that the action should be open with no parameters
What am I doing wrong?
I think I may have answered my question since it appears that Rails can only have one index per controller so I couldn't define another view that acts as an index.
So now what I want to do is pass a parameter so I can apply either an open scope or an expired scope

After deleting a record in Rails 3, the refreshed view isn't updated

I'm dealing with a basic one to many relation where I'm deleting a record on the many side. The models are "places" and "search_terms" where places has_many search_terms. When I create a new record, the view is updated and the new search_term appended to the list. However, when I delete a search_term record the view is not refreshed even though it deletes the record and runs the "show" method for Place.
I'm quite new to rails 3 so can't really figure out whats going on here...
Cheers,
Gearoid.
Edit: the search_terms controller destroy method:
def destroy
#search_term = SearchTerm.find(params[:id])
#search_term.destroy
#place = Place.find(params[:place_id])
redirect_to place_path(#place)
end
The places controller show method:
def show
#place = Place.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #place }
end
end
I might be misunderstanding you, could you post your controller's code?
Is this happening over ajax? If not, can you redirect to the Show instead of just re-rendering it? That's probably a preferred experience for the user anyway.
UPDATE
Ok, if this is going over ajax, then the problem is simple. Your destroy action is only expecting a normal browser event and doing a redirect_to call. The ajax call doesn't know how to handle it and just sits there. You can probably see the redirect code in something like Firebug.
I'm not super familiar with jquery-rails (I prefer to write all my js myself because I'm anal). You can have the destroy action return a js format like so:
def destroy
#search_term = SearchTerm.find(params[:id])
#search_term.destroy
#place = Place.find(params[:place_id])
respond_to do |format|
format.html { redirect_to place_path(#place) }
format.js { render :nothing => true }
end
end
That will give the ajax caller the ok signal that it has done its thing. Your javascript will still have to intelligently handle this response though, like remove the element from the DOM.

How would you implement a save and create another feature on a model in rails 3?

I'm trying to implement a save and create another instance of a model but I can't understand how routing works.
def save_and_create
#book_condition = BookCondition.new(params[:book])
if #book_condition.save
flash[:notice] = t('app.messages.success')
redirect_to new_book_condition_url
else
render :action => 'new'
end
end
Above is what I wrote. How would you do it? How would you route it?
Thanks

Rails3 and Respond_with problem

I have an application, on which I have two user interfaces.
The first one is for normal users and the second one is for iphone users.
Everything was working fine until i refactored my code within controller to use the respond_with declarative instead of respond_to.
The application is still working for the html interface(:format => :html) but not on the iphone interface(:format => :iphone).
On the iphone, when I do the following action (:index, :new, :edit, :show) it works.
But when i do (:create, :update, :destroy), I get errors saying the template is not found(create.iphone.haml for example).
On my controller I have
respond_to :html, :iphone
And then for example, the edit and the update action
def edit
#refund = Refund.find(params[:id])
respond_with(#refund)
end
def update
#refund = Refund.find(params[:id])
if #refund.update_attributes(params[:refund])
flash[:notice] = 'Refund was successfully updated.'
end
respond_with(#refund, :location => project_refunds_path(#project))
end
In fact, I would like the :iphone format is handle as :html is ... and not by calling the to_format method as it is specified into the doc.
Solved it by myself.
Just need to add this to an initializer file :
ActionController::Responder.class_eval do
alias :to_iphone :to_html
end
What if you do:
respond_with(#refund, :location => project_refunds_path(#project)) do |format|
format.iphone { whatever you had here before refactoring }
end

How do I make a route to a custom controller action in Rails 3?

I'm new to Rails, and a bit confused about routes:
I have a Devices controller:
#devices_controllers.rb
class DevicesController < ApplicationController
def index
#devices = Device.all
end
def show
#device = Device.find(params[:id])
end
def new
#device = Device.new
end
def create
#device = Device.new(params[:device])
if #device.save
flash[:notice] = "Successfully created device."
redirect_to #device
else
render :action => 'new'
end
end
def edit
#device = Device.find(params[:id])
end
def update
#device = Device.find(params[:id])
if #device.update_attributes(params[:device])
flash[:notice] = "Successfully updated device."
redirect_to #device
else
render :action => 'edit'
end
end
def destroy
#device = Device.find(params[:id])
#device.destroy
flash[:notice] = "Successfully destroyed device."
redirect_to devices_url
end
def custom_action
"Success"
end
I'd like to access the "custom_action" action via a url like this:
http://foo.bar/devices/custom_action
I've added this line to my routes.rb file:
match 'devices/custom_action' => 'devices#custom_action'
However, when I try the URL in the browser, I get this error:
ActiveRecord::RecordNotFound in DevicesController#show
Couldn't find Device with ID=custom_action
It seems to be going to #show action instead of #custom_action. If a user id is not supplied, and I go to http://foo.bar/devices/custom_action, I'd like it to go #custom_action.
I've read Rails Routing from the Outside, but still can't still seem to figure out the problem.
I think the problem may be because of the order in which you have defined your routes.
I suspect you have resources :devices in your routes.rb. In addition, I suspect you have defined your custom route after this. If you type rake routes into your console/terminal, you will see that there is already a route defined for the following pattern:
GET /devices/:id
This route is a product of resources :devices, which is taking precedence over your custom route. Referring back to the Edge Guides, specifically in 1.1. Connecting URLs to Code, it states that the request will be dispatched to the first matching route. So a simple fix would be to define your custom route before resources :devices.