Route Error: undefined local variable or method `events_path' - ruby

In rails 4.0.2, I am trying to implement a calendar feature along with the events.
I have referred from https://github.com/vinsol/fullcalendar-rails-engine.
But I am facing some route issue like
undefined local variable or method `events_path' for #<#<Class:0x9b8fed0>:0xb5e2e9a0>
In routes.rb file i have added,
root 'welcome#index'
mount FullcalendarEngine::Engine => "/fullcalendar_engine"
Generated routes are,
$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
fullcalendar_engine /fullcalendar_engine FullcalendarEngine::Engine
Routes for FullcalendarEngine::Engine:
root GET / fullcalendar_engine/events#index
get_events_events GET /events/get_events(.:format) fullcalendar_engine/events#get_events
move_event POST /events/:id/move(.:format) fullcalendar_engine/events#move
resize_event POST /events/:id/resize(.:format) fullcalendar_engine/events#resize
events GET /events(.:format) fullcalendar_engine/events#index
POST /events(.:format) fullcalendar_engine/events#create
new_event GET /events/new(.:format) fullcalendar_engine/events#new
edit_event GET /events/:id/edit(.:format) fullcalendar_engine/events#edit
event GET /events/:id(.:format) fullcalendar_engine/events#show
PATCH /events/:id(.:format) fullcalendar_engine/events#update
PUT /events/:id(.:format) fullcalendar_engine/events#update
DELETE /events/:id(.:format) fullcalendar_engine/events#destroy
In view, welcome/index.html.erb,
<%= link_to "Show Calendar", events_path %>
In fullcalendar_engine/events/index.html.erb,
<p><%= link_to 'Create Event', 'javascript:void()', id: 'new_event' %></p>
<div><div class='calendar'></div></div>
<div id="event_desc_dialog" class="dialog" style="display:none;"></div>
<div id="create_event_dialog" class="dialog" style ="display:none;">
<%= render 'form' %>
</div>
<script>
$(document).ready(function(){
$('.calendar').fullCalendar(full_calendar_options);
// page is now ready, initialize the calendar...
$('#new_event').click(function(event) {
FullcalendarEngine.Form.display()
event.preventDefault();
});
});
</script>

I guess you are using that link to show action,then it should be something like this
<%= link_to "Show Calendar", events_path(#event) %>
From the rake routes you provided
event GET /events/:id(.:format) fullcalendar_engine/events#show
the show route requires an :id,which is currently missing in your link
Update
You are calling a different controller action from the view which is not part of that controller action,so you should be giving it like this
<%= link_to "Show Calendar", :controller => 'events', :action => 'index' %>

Related

Saving data in ruby on Rails without resources

Its bit difficult for me to explain what I want, but still I will try my best.
I created a form in Rails where user can fill certain fields. Now once these fields are filled, I know that I can use resources in Router and define a create method in Controller that will save the data to database.
However what I want is to pass data saved in the form to my controller. Then create a custom method in controller that will be just like traditional Create method, but instead of passing parameters using Resource method, I want to pass them as parameter. Is it even possible in Rails:
This is my current View to create form:
<h1> Please add a new product</h1>
<%= form_for #product do |p| %>
<p>
<%= p.label 'Product Name' %><br/>
<%= p.text_field :product_name %><br/>
</p>
<p>
<%= p.label 'Description' %><br/>
<%= p.text_area :description %><br/>
</p>
<p>
<%= p.label 'Price' %><br/>
<%= p.text_field :price %><br/>
</p>
<p>
<%= p.label 'Rating' %><br/>
<%= p.text_field :rating %><br/>
</p>
<% end %>
So may be I am using In Built form in Ruby, but I just want to pass parameters from View to Controller's method.
Thanks for help !!!
I will help you in solving your problem.
Follow these steps:
Create your route in routes.rb:
get "/create_product" => 'products#create_product', as: :create_product
or if you want to pass params through post method:
post "/create_product" => 'products#create_product', as: :create_product
Now change your view file according to the new route helper:
form_for (#products, url:{:controller=>'products', :action=>'create_product'}, html:{method:'post'})
Now the last step modify your controller:
def create_product
#your form values are avaible here in params variable
pp params
puts params[:Price]
#save your params into ur db
end
Note: I assumed that you already have product.rb model

set config/routes for accept parameters

I need set the route en config/routes for accept this route when I push the submit button in the form
<% form_tag(:controller => "students", :action => "index", :q1 => "v1", :q2 => "v2") %>
<%= submit_tag "Send" %>
<% end %>
Browser
Routing Error
No route matches {:controller=>"students", :q1=>"v1", :q2=>"v2"}
Try running rake routes for more information on available routes.
Why are you creating a form that gets submitted at index action?
It is by default a get action and form submission is a post request. So, there are no routes defined for this request.
By convention, it should get submitted to a Post action.

redirect to another URL using submit button in ruby

I have submit button and i want to redirect in another URL (hard coded) this URL
https://www.ccavenue.com/shopzone/cc_details.jsp
my code :
<%= form_tag #ccavanue do |f| , url => "https://www.ccavenue.com/shopzone/cc_details.jsp", :html => :method => "put" %>
<%= hidden_field_tag :my_field, #MerchantId, :id => 'merchant_id' %>
<%= submit_tag "Click Me" %>
<% end %>
i want to redirect another website URL with this submit button . please guided me.
Change your code to following:
<%= form_for #ccavanue, url: "https://www.ccavenue.com/shopzone/cc_details.jsp" do |f| %>
<%= f.hidden_field :my_field, #MerchantId, :id => 'merchant_id' %>
<%= f.submit "Click Me" %>
<% end %>
In Rails a form is designed to create or update a resource and reflects the identity of the resource in several ways:
The url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource),
Input fields should be named in such a way that in the controller their values appear in the appropriate places within the params hash, and
For an existing record, when the form is initially displayed, input fields corresponding to attributes of the resource should show the current values of those attributes.
In Rails this is achieved by creating form using form_for where:
If we want to create any object we use POST method within url and PUT method if we are trying to update an existing record.
Rails framework is smart enough to use POST or PUT method by itself looking at the url of the form. So in this case we need not use method parameter within form_for url.
Probably you can start with Michael Hartl's tutorial

Created objects do not save in database

What did I do:
create a new ruby app rails new campaigns_manager
move to campaigns_manager directory, generate first scaffold campaigns
rails generate scaffold campaign name:string and migrate
add root route root to: 'campaigns#index', as: 'campaigns'
Such a few strings of code. Then I start server to test it. Everything looks and works ok, but when I try to add a new campaign, nothing saves. After pressing "Create campaign" button I receive in console something like that:
Started POST "/" for 127.0.0.1 at 2012-04-25 22:30:40 +0300
Processing by CampaignsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sKom3YBDEbOcqbSt3gLGWPqBNeqkEw6M59hlwrmH4tM=", "campaign"=>{"name"=>"test"}, "commit"=>"Create Campaign"}
Campaign Load (0.3ms) SELECT "campaigns".* FROM "campaigns"
Rendered campaigns/index.html.erb within layouts/application (0.9ms)
Completed 200 OK in 37ms (Views: 35.4ms | ActiveRecord: 0.3ms)
then I redirect to campaigns list and there is no new campaign. Table campaigns in db\development.sqlite3 is empty.
Furthermore, I add print methods to campaigns controller to check if I call correct methods. And I see, that create method is not called when I press "Create campaign" button.
My campaigns/new view:
<h1>New campaign</h1>
<%= form_for(#campaign) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', campaigns_path %>
Why cannot I save my campaigns to database?
The problem is in the routes.rb file.
root to: 'campaigns#index', as: 'campaigns'
The alias as: 'campaigns' tries to consume all requests. There is no need to add an alias to your root directive, it already has one by default: as: 'root'
Your routes.rb file has to look something like this:
resources :campaigns
root to: 'campaigns#index'
More on Routes in Rails Guides
It would help to see the view code in campaigns/new, but clearly the form there is posting to /, which you can see from the log, is being handled by the index action (which is as it should be according to your routes). You have to make sure your form in campaigns/new is posting to /campaigns (which in Rails RESTful routing resolves to the create action). If you're using form_for (which I think is what the scaffold generates), it should look something like this:
<%= form_for #campaign do |f| %>

Rails3 routing error with ajax

UPDATED CODE at the bottom
I am creating a story voting app via Simply Rails 2 book. I am getting this error when I click the button to vote up a story:
No route matches "/stories/4-pure-css-icons-showcase"
My routing file looks like this:
Shovell::Application.routes.draw do
get "votes/create"
root :to => "stories#index"
resources :stories do
resources :votes
end
end
votes_controller.rb:
class VotesController < ApplicationController
def create
#story = Story.find(params[:story_id])
#story.votes.create
end
end
create.rsj :
page.replace_html 'vote_score', "Score: #{#story.votes.size}"
page[:vote_score].visual_effect :highlight
show.html.erb:
<h2>
<span id="vote_score">
Score: <%= #story.votes.size %>
</span>
<%= #story.name %>
</h2>
<p>
<%= link_to #story.link, #story.link %>
</p>
<div id="vote_form">
<%= form_tag :url => story_votes_path(#story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
</div>
story.rb :
class Story < ActiveRecord::Base
validates_presence_of :name, :link
has_many :votes
def to_param
"#{id}-#{name.gsub(/\W/, '-').downcase}"
end
end
I've been working through a number of other errors before this having to do with deprecated code and so forth, so I feel somewhat lost at the moment. It seems like it should just be a routing a issue, but since I've been working through AJAX errors that also have to do with the vote function I wanted to post those files just in case it was more than routing.
It says no route matches "/stories/4-pure-css-icons-showcase" but when I visit "/stories" (my root) and click on the link to take me to "/stories/4-pure-css-icons-showcase" it works fine, however after clicking on the vote button I get this error. As you could probably tell after reading the code, it is suppose to update the vote count and do a :highlight via ajax.
UPDATE:
Changed code (all changes are per Sam's advice):
routes:
Shovell::Application.routes.draw do
resources :votes
root :to => "stories#index"
resources :stories do
resources :votes
end
show.html.erb:
<div id="vote_form">
<%= form_tag :url => new_story_vote_path(#story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
</div>
votes_controller.rb
class VotesController < ApplicationController
def create
#story = Story.find(params[:story_id])
#story.votes.create
respond_to do |format|
format.html
format.js
end
end
The problem is still exactly the same, but I think (read: hope) we are making progress!
The scenario: My index (/stories) page randomly displays a story from the database, when you click the link it takes you to the story's internal page (ex. /stories/2-sitepoint-forums) on this page it displays the number of votes the story has and has a button to vote for it. When you click the vote button it is suppose to use ajax to update the #story.vote.size and use a :highlight visual effect. However, the problem is that when you click the vote button the page changes to a "Routing Error" page which displays:
No route matches "/stories/2-sitepoint-forums"
Its weird to me because you can in fact be routed to that address and you are from the link on the first page...
Here is the error in the console:
Started POST "/stories/2-sitepoint-forums?url=%2Fstories%2F2-sitepoint-forums%2F
votes%2F2-sitepoint-forums&remote=true" for 127.0.0.1 at 2010-11-08 16:30:17 -08
00
ActionController::RoutingError (No route matches "/stories/2-sitepoint-forums"):
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0.rc2/lib/action_dis
patch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0m
s)
Im not sure if this is any more telling, but I thought I'd add it incase.
New:
I have not been able to solve this problem as of yet. Because I still don't feel like I completely understand the issue I have decided to move over to the Ruby on Rails 3 Tutorial Book online and see if I can't figure it out while working through it. Since I was planning to do it next anyway (I have plans to combine both apps later) it appears now is the time.
<%= form_tag :url => new_story_vote_path(#story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
That should send it to the create action.
def create
#story = Story.find(params[:story_id])
#story.votes.create
respond_to do |format|
format.html
format.js
end
end
And that should take care of your ajax.
take this get "votes/create" out of your routes
and add this
map.resources :votes
I'm not familiar with using form_tag and :remote (as I normally just write jQuery for stuff like this) but a couple of things definitely pop out at me with what you're doing here that may help you resolve the issue.
First of all, I think you can rework the way you set up a vote and thus the way you set up the form for the vote. In the stories controller, for the show action, I'd set up the vote right away:
#vote = Vote.new(:story_id => #story.id)
This lets you set up your form as so:
= form_for(#vote), :remote => true do |f|
= hidden_field f.story_id
= submit_tag "Vote"
This is both a cleaner way of doing things, in my opinion, but also may fix the general issue you are dealing with, because you are now passing data with the form (the hidden field) in your POST request. Rails will behave unexpectedly if you perform AJAX POST requests that do not actually submit data.
In other words, your original form is likely running as an AJAX POST request but it would have worked better as an AJAX GET request, since it is not actually submitting data, it is simply "hitting" an URL.
I am not sure if you found the answer to your problem yet, but I wanted to post for others that may be looking for an answer similar to yours.
The code:
<%= form_tag :url => new_story_vote_path(#story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
will result in /stories/:story_id/votes/new url with a :post request. It won't work because the new route is a :get method request. If you wanted to go to the new method, you'll need to tell the form to use the get http method.
<%= form_tag :url => new_story_vote_path(#story), :remote => true, :html => { :method => :get } do %>
<%= submit_tag 'shove it' %>
<% end %>
However, I think that you are wanting to route to the create method in your controller. I would do something like:
<%= form_tag :url => story_votes_path(#story), :remote => true do %>
<%= submit_tag 'shove it' %>
<% end %>
This should route correctly to the create method in your VotesController.

Resources