syntax error, unexpected keyword_ensure, expecting $end - ruby-on-rails

I've been trying to figure out this error for about 3 hours and have found other examples with same title, but didn't find any solution yet..
The website says:
syntax error, unexpected keyword_ensure, expecting $end, in /controllers/carts_controller.rb Line 17
respond_to do |format| /////// <-- says here the problem
The rails server says the problem is:
views/carts/show.html.erb:21: syntax error, unexpected keyword_ensure, expecting $end
...and here is the whole code:
/controllers/carts_controller.rb
class CartsController < ApplicationController
def index
#carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #carts }
end
end
def show
begin
#cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access ionvalid cart #{params[:id]}"
redirect_to store_url, :notice => 'Invalid cart'
else
respond_to do |format|
format.html
format.xml { render :xml => #cart }
end
end
end
def new
#cart = Cart.new
respond_to do |format|
format.html
format.xml { render :xml => #cart }
end
end
def edit
#cart = Cart.find(params[:id])
end
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render action: 'show', status: :created, location: #cart }
else
format.html { render action: 'new' }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
def update
#cart = Cart.find(params[:id])
respond_to do |format|
if #cart.update_attributes(params[:cart])
format.html { redirect_to(#cart, :notice => 'Cart was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #cart.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#cart = current_cart
#cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html {redirect_to(store_url, :notice => 'Your cart is currently empty') }
format.xml { head :ok }
end
end
end
indented all code
the view:
views/carts/show.html.erb
<div class="cart_title">Your Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, :method => :delete, :confirm => 'Are you sure?' %>
SOLUTION FOUND:
The problem was on views/carts/show.html.erb. Changing cart to #cart solved the problem.
Thanks for the help.

Related

Ruby on Rails - Passing Parameters between methods

Routes File:
resources :tournaments do
resources :game, :only => [:new, :index, :create, :update, :destroy]
end
Rake Routes shows:
new_tournament_game GET /tournaments/:tournament_id/game/new(.:format) game#new
I call:
<td><%= link_to 'Add Game', new_tournament_game_path(tournament) %></td>
Game Model:
Takes me to the view game view with the URL:
http:// local host:3000/tournaments/2/game/new
And view:
<h1>New Game in <%= #tournament.name %> Tournament</h1>
<fieldset>
<%= form_for [:tournament, #game], :url => tournament_game_index_path do |f| %>
<table>
<td>
.... More fields .....
</td>
<div class="form-actions">
<%= f.submit "Create %>
</div>
<% end %>
When create is clicked it yields the error:
undefined method `game_url' for #<GamesController:0xb6131e40>
Questions:
Should I be using nested routes or hidden fields?
Do I need a separate tournament_game controller / view to handle the tournament game calls?
How do I get it to look for the correct create route when clicking the Create button?
When I want to have a relationship between two tables, do I only need the nested resource and the has_many / belongs_to calls or do I still need a foreign key column such as Tournament?
Sorry for all of the questions in one thread. Any help you can offer would be greatly appreciated!
Thank you.
Edit:
The error references line 39 which would be the line for the create controller.
class GamesController < ApplicationController
# GET /game
# GET /game.json
def index
#game = Game.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #game }
end
end
# GET /game/1
# GET /game/1.json
def show
#game = Game.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #game }
end
end
# GET /game/new
# GET /game/new.json
def new
#game = Game.new
#tournament = Tournament.find(params[:tournament_id])
end
# GET /game/1/edit
def edit
#game = Game.find(params[:id])
end
# POST /game
# POST /game.json
def create
#tournament = Tournament.find(params[:tournament_id])
#game = #tournament.game.build(params[:game])
respond_to do |format|
if params[:commit] != 'Cancel'
if #game.save
format.html { redirect_to #game, notice: 'Game was successfully created.' }
format.json { render json: #game, status: :created, location: #game }
format.json { render json: #game }
else
format.html { render action: "new" }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
else
format.html { redirect_to #game, alert: 'Game was not updated.' }
end
end
end
# PUT /game/1
# PUT /game/1.json
def update
#game = Game.find(params[:id])
respond_to do |format|
if params[:commit] != 'Cancel'
if #game.update_attributes(params[:game])
format.html { redirect_to #game, notice: 'Game was successfully updated.' }
format.json { render json: #game }
else
format.html { render action: "edit" }
format.json { render json: #game.errors, status: :unprocessable_entity }
end
else
format.html { redirect_to #game, alert: 'Game was not updated.' }
end
end
end
# DELETE /game/1
# DELETE /game/1.json
def destroy
#game = Game.find(params[:id])
#game.destroy
respond_to do |format|
format.html { redirect_to game_url }
format.json { head :no_content }
end
end
end
Try this:
<%= form_for #game, :url => tournament_games_path(#tournament) do |f| %>
This will call the create method of games controller
Game Controller:
def create
#tournament = Tournament.find(params[:tournament_id])
#game = #tournament.games.build(params[:game])
if #game.save
flash[:success] = "Game created successfully"
redirect_to tournaments_path
else
render new_tournament_game_path
end
end
Routes:
resources :tournaments do
resources :games, :only => [:new, :index, :create, :update, :destroy]
end

render error messages with js form rails

I changed my form to remote, and while the form now works, the error messages are no longer displaying if there's an error.
<%= render 'shared/error_messages' %>
is there a good wear to get the messages to show again?
below is my controller...
thank you.
respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { render :js => #post.errors }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end
respond_to do |format|
if #post.save
format.js { render :js => "window.location = '#{edit_post_path #post}'" }
format.html { redirect_to [:edit, #post] }
else
format.js { }
format.html { redirect_to '/', :error => "Could not save comment" }
end
end
# update.js.erb
$(document).find("form").prepend('<%= escape_javascript(render("shared/error_messages", :formats => [:html])) %>');

Partial Rendering of another controller's view ruby on rails

I am using twitter Boostraps tabbable feature found here:
http://twitter.github.com/bootstrap/components.html#navs
And within this navigation content window, I am trying to render a view that displays a "course". This view found in views/courses/_show.html.erb looks like this:
<div class="center hero-unit">
<h1><%= #course.course_name %></h1>
<%= link_to 'New Question Set',new_answer_path(:course_ID => #course.id), :class => "btn btn-large btn-primary" %>
<%= link_to 'Launch Session!', edit_course_path, :class => "btn btn-large btn-primary" %>
</div>
I am trying to render it and failing with the following code in views/instructor/show.html.erb
<% courses.each do |c| %>
<div class="tab-pane" id="<%=c.course_name%>">
<%= render :partial => 'courses/show', :locals => {#course=>c} %>
</div>
I get the following error:
/app/views/courses/_show.html.erb:1: syntax error, unexpected '=',
expecting keyword_end ...tput_buffer = #output_buffer; =
local_assigns[:];show = loca... ... ^
/app/views/courses/_show.html.erb:1: syntax error, unexpected ']',
expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or
tSTRING_END ...tput_buffer; = local_assigns[:];show =
local_assigns[:show];... ...
saying it's failing at line 1 of my courses/_show.html.erb
My Course Controller looks like this:
class CoursesController < ApplicationController
# GET /courses
# GET /courses.json
def index
#courses = Course.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #courses }
end
end
# GET /courses/1
# GET /courses/1.json
def show
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #course }
end
end
# GET /courses/new
# GET /courses/new.json
def new
#course = Course.new(instructor_ID: params[:instructor_ID])
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #course }
end
end
# GET /courses/1/edit
def edit
#course = Course.find(params[:id])
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(params[:course])
respond_to do |format|
if #course.save
format.html { redirect_to #course, :notice => 'Course was successfully created.' }
format.json { render :json => #course, :status => :created, :location => #course }
else
format.html { render :action => "new" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
# PUT /courses/1
# PUT /courses/1.json
def update
#course = Course.find(params[:id])
respond_to do |format|
if #course.update_attributes(params[:course])
format.html { redirect_to #course, :notice => 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
Note: I have ommited some of the methods like delete in my controller to save space.
Any Ideas?!
In #course=>c change the # to a colon.

Ruby - error rendering a view referencing a loop that I never wrote

I have this controller:
class AlexesController < ApplicationController
# GET /alexes
# GET /alexes.json
def index
##alexes = Alex.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #alexes }
end
end
# GET /alexes/1
# GET /alexes/1.json
def show
# #alex = Alex.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #alex }
end
end
# GET /alexes/new
# GET /alexes/new.json
def new
#alex = Alex.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #alex }
end
end
# GET /alexes/1/edit
def edit
#alex = Alex.find(params[:id])
end
# POST /alexes
# POST /alexes.json
def create
#alex = Alex.new(params[:alex])
respond_to do |format|
if #alex.save
format.html { redirect_to #alex, notice: 'Alex was successfully created.' }
format.json { render json: #alex, status: :created, location: #alex }
else
format.html { render action: "new" }
format.json { render json: #alex.errors, status: :unprocessable_entity }
end
end
end
# PUT /alexes/1
# PUT /alexes/1.json
def update
#alex = Alex.find(params[:id])
respond_to do |format|
if #alex.update_attributes(params[:alex])
format.html { redirect_to #alex, notice: 'Alex was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #alex.errors, status: :unprocessable_entity }
end
end
end
# DELETE /alexes/1
# DELETE /alexes/1.json
def destroy
#alex = Alex.find(params[:id])
#alex.destroy
respond_to do |format|
format.html { redirect_to alexes_url }
format.json { head :no_content }
end
end
end
It gets called when this link gets pressed:
<%= link_to "Alex Link", alexes_path(#alex) %>
so I am assuming that the get-all part of the controller would get invoked, and I commented out lines in the controller that I thought would get invoked, but I still get this error:
undefined method `each' for nil:NilClass
from line 10:
7: <th></th>
8: </tr>
9:
10: <% #alexes.each do |alex| %>
11: <tr>
12: <td><%= link_to 'Show', alex %></td>
13: <td><%= link_to 'Edit', edit_alex_path(alex) %></td>
Any idea where the problem is happening?
Thanks!
<%= link_to "Alex Link", alexes_path(#alex) %>
=>
<%= link_to "Alex Link", alex_path(#alex) %>
or
<%= link_to "Alex Link", #alex %>

The action could not be found for controller

Having gone through my code I have a separate problem from my original question and rather than writing a new question. I will leave the old part at the bottom of this and post the new problem here. I do this because they are closely related.
New:
Im getting an error message saying
Unknown action
The action 'response' could not be found for CrawlerController
I'll keep it simple but the code for model, controller and routes are below in the previous question.
A basic run down is response is a def within CrawlerController as is add_Request.
The routes are matched as such:
match "/requests/new" => "crawler#add_Request"
match 'requests/:id' => 'crawler#response'
Here is controller code as per user request:
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.html
format.js { render :json => #request }
end
end
def show
#request = Request.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #request }
format.json{
render :json => #request.to_json
}
end
end
end
please recheck code of controller as I can see it
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.json {render :#request.to_json}
end
end
so one end is missing an your response action is defined inside add_Request

Resources