In my controller I pass date from method create to method index. How can I pass it back from index to create (for new create)?
def index
#date = params[:date].
end
def create
<<<NEED to get #date from index here>>>
#entry = Entry.new(:input => input, :user => current_user, :time => #date)
respond_to do |format|
if #entry.save
format.html { redirect_to(:action => "index", :edit => true) }
end
end
Why do you need to get it from index?
Keep it in a hidden form variable and submit it, etc. You can't get it from index.
Related
I have three html pages.
In the first page, I view the list of data.
In the second page, I view the data of a particular element of the list.
In the third page, the user can edit data of a particular element of the list.
When the user submits the 'form', how can I redirect the user in the second page? I tried in this way:
render :action => "show_details",:id=>params[:id]
It works. The link is correct. But the page is not opened if I do not refresh the page.
UPDATE I
I write my code in this action in the reports controller:
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action => "show_details",:id=>params[:id]
end
UPDATE II
Reports controller code:
class ReportsController < ApplicationController
before_filter :authenticate_user, :only => [:index,:show,:show_details,:new]
def stateDialog
render :stateDialog, :current_state=>params[:current_state]
end
def setFixed
rs=Report.find(params[:id])
rs.state ="1"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def setNotFixed
rs=Report.find(params[:id])
rs.state ="0"
rs.save
render :action=>"show_details",:id=>params[:id]
end
def edit
#report=Report.find(params[:id])
end
def update
#report = Report.find(params[:id])
if #report.update_attributes(params[:report])
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
else
flash[:notice]=''
render :action=>"show_details",:id=>params[:id]
end
end
def deleteDialog
render "deleteDialog"
end
def focus_maps
render "focus_maps"
end
def delete
Report.find(params[:id]).destroy
render "show"
end
def index
#report=Report.new
end
def logged
render "new"
end
def show
render params[:page]
end
def new
#report=Report.new
end
def show_details
render "show_details"
end
def create
#report=Report.new( params[:report] )
if #report.save
flash[:notice]='Segnalazione avvenuta!'
else
flash[:notice]='Impossibile sottoporre la segnalazione!'
end
render "show"
end
end
I found advice that params must be filled before render calling, like this:
#id = params[:id]
render :action => 'show_details'
This solution is works for me, try it
If you want to redirect user somewhere you should use redirect_to
redirect_to action: 'show_details', id: params[:id]
I have an index page which has a table in it. I'd like a drop down box that will display various ways to order the table. like this:
'<%= f.select :order_by [service_date, Car_name, etc.]'
I'm not sure what to put in my controller to be able to read the value in the drop box and then submit the new order
#index_controller
def index
#cars = Car.find(:all, :order => 'value of drop down box in here')
#cars_available = Car.where(:available => true, :booked => false)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #cars}
end
end
any ideas?
It works like any other form parameter:
# GET
def index
#cars = Car.find(:all, :order => params[:car][:order_by])
#cars_available = Car.where(:available => true, :booked => false)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #cars}
end
end
You should define an instance method order_by in your Car model to return a default value:
def order_by
"your default order_by" #will determine the default selected order_by on a form
end
PS You need this method, or atleast attr_reader :order_by or attr_accessor :order_by, or else you will get an Execption for f.select :order_by
I have a page that I want to display all of the entries in the database for a given week. Each entry in the database has an :entrydate field that contains the date that the entry is for.
In /config/routes.rb:
match "reports/*date" => "reports#index", :defaults => { :date => DateTime.now.strftime('%m/%d/%Y') }
In /app/controllers/reports_controller.rb:
def index
#reports = Report.where(:entrydate => Date.strptime(params[:date], '%m/%d/%Y').beginning_of_week..Date.strptime(params[:date], '%m/%d/%Y').end_of_week)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #reports }
format.js
end
end
However, when I try to run the page localhost:3000/reports, I get an error:
can't dup NilClass
/app/jruby-1.6.5.1/lib/ruby/1.8/date/format.rb:599:in `_strptime'
/app/jruby-1.6.5.1/lib/ruby/1.8/date.rb:979:in `strptime'
app/controllers/reports_controller.rb:7:in `index'
It works fine if I input a date such as localhost:3000/reports/10/29/2012.
It appears as though your default value is not getting set properly. Perhaps this is because it is not a constant?
Anyway, you probably don't want to set the default like this anyway because you have less control over when the default gets set.
I think something like this would be a better approach:
def index
my_date = params[:date] || DateTime.now.strftime('%m/%d/%Y')
#reports = Report.where(:entrydate => Date.strptime(my_date, '%m/%d/%Y').beginning_of_week..Date.strptime(my_date, '%m/%d/%Y').end_of_week)
It looks like your variable may be getting lost between routes and the controller. Maybe try declaring a default date within the controller itself?
def index
params[:date].blank? ? date = DateTime.now.strftime('%m/%d/%Y') : date = params[:date]
#reports = Report.where(:entrydate => Date.strptime(date, '%m/%d/%Y').beginning_of_week..Date.strptime(date, '%m/%d/%Y').end_of_week)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #reports }
format.js
end
end
The functionality I'm trying to build allows Users to Visit a Restaurant.
I have Users, Locations, and Restaurants models.
Locations have many Restaurants.
I've created a Visits model with user_id and restaurant_id attributes, and a visits_controller with create and destroy methods.
Thing is, I can't create an actual Visit record. Any thoughts on how I can accomplish this? Or am I going about it the wrong way.
Routing Error
No route matches {:controller=>"restaurants", :location_id=>nil}
Code:
Routes:
location_restaurant_visits POST /locations/:location_id/restaurants/:restaurant_id/visits(.:format) visits#create
location_restaurant_visit DELETE /locations/:location_id/restaurants/:restaurant_id/visits/:id(.:format) visits#destroy
Model:
class Visit < ActiveRecord::Base
attr_accessible :restaurant_id, :user_id
belongs_to :user
belongs_to :restaurant
end
View:
<% #restaurants.each do |restaurant| %>
<%= link_to 'Visit', location_restaurant_visits_path(current_user.id, restaurant.id), method: :create %>
<% #visit = Visit.find_by_user_id_and_restaurant_id(current_user.id, restaurant.id) %>
<%= #visit != nil ? "true" : "false" %>
<% end %>
Controller:
class VisitsController < ApplicationController
before_filter :find_restaurant
before_filter :find_user
def create
#visit = Visit.create(params[:user_id => #user.id, :restaurant_id => #restaurant.id])
respond_to do |format|
if #visit.save
format.html { redirect_to location_restaurants_path(#location), notice: 'Visit created.' }
format.json { render json: #visit, status: :created, location: #visit }
else
format.html { render action: "new" }
format.json { render json: #visit.errors, status: :unprocessable_entity }
end
end
end
def destroy
#visit = Visit.find(params[:user_id => #user.id, :restaurant_id => #restaurant.id])
#restaurant.destroy
respond_to do |format|
format.html { redirect_to location_restaurants_path(#restaurant.location_id), notice: 'Unvisited.' }
format.json { head :no_content }
end
end
private
def find_restaurant
#restaurant = Restaurant.find(params[:restaurant_id])
end
def find_user
#user = current_user
end
end
I see a lot of problems here. The first is this line of code in your VisitController's create action (and identical line in your destroy action):
#visit = Visit.create(params[:user_id => #user.id, :restaurant_id => #restaurant.id])
params is a hash, so you should be passing it a key (if anything), not a bunch of key => value bindings. What you probably meant was:
#visit = Visit.create(:user_id => #user.id, :restaurant_id => #restaurant.id)
Note that you initialize #user and #restaurant in before filter methods, so you don't need to access params here.
This line of code is still a bit strange, though, because you are creating a record and then a few lines later you are saving it (if #visit.save). This is redundant: Visit.create initiates and saves the record, so saving it afterwards is pretty much meaningless. What you probably want to do is first initiate a new Visit with Visit.new, then save that:
def create
#visit = Visit.new(:user_id => #user.id, :restaurant_id => #restaurant.id)
respond_to do |format|
if #visit.save
...
The next thing I notice is that you have not initiated a #location in your create action, but you then reference it here:
format.html { redirect_to location_restaurants_path(#location), notice: 'Visit created.' }
Since you will need the location for every restaurant route (since restaurant is a nested resource), you might as well create a method and before_filter for it, like you have with find_restaurant:
before_filter :find_location
...
def find_location
#location = Location.find(params[:location_id])
end
The next problem is that in your view your location_restaurant_path is passed the id of current_user and of restaurant. There are two problems here. First of all the first argument should be a location, not a user (matching the order in location_restaurant_path). The next problem is that for the _path methods, you have to pass the actual object, not the object's id. Finally, you have method: :create, but the method here is referring to the HTTP method, so what you want is method: :post:
link_to 'Visit', location_restaurant_visits_path(#location, restaurant.id), method: :post
You'll have to add a find_location before filter to your RestaurantController to make #location available in the view here.
There may be other problems, but these are some things to start with.
location_id is nil and the path definition doesn't say (/:location_id) forcing a non-nil value there in order to route to that path; create a new route without location_id if you can derive it from a child's attribute (i.e. a restaurant_id refers to a Restaurant which already knows its own location_id).
This should have been a relatively simple one but I must be making a mistake with my routes or something. I want to return an active record as json based on an id. So heres what I have and in my eyes it should have worked.
The route:
match '/repository/infoid/(.:id)(.:format)' =>'repo#infoID', :via =>:get
The def within the controller
def infoID
puts (params[:id])
#specificObject = myObject.find_by_id(params[:id])
respond_to do |format|
format.xml{
render :xml =>
{
:returnedObject => #specificObject
}
}
end
end
Why is it that when I go to my address of http://127.0.0.1:3008/repository/infoid/1.xml
I get no route found for /infoid/1.xml
get '/repository/infoid/:id' => 'repo#infoID'
little refacrtoring for controller
def infoID
#specificObject = MyObject.find(params[:id])
respond_to do |format|
format.html{}
format.xml{
render :xml => #specificObject
}
end
end