I am creating a Rails 4.2.6 /MongoDb app. I created a new action manually called 'calluser', made the change in routes to including it:
resources :companies do
member do
get 'calluser'
end
end
I can see it when I execute the 'rake routes' command:
call_user_company GET /companies/:id/call_user(.:format) companies#call_user
However, When I redirect from the controller:
if #company.save
format.html { redirect_to calluser_company(#company), notice: 'Company was successfully created.' }
format.json { render action: 'show', status: :created, location: #company }
else
format.html { render action: 'new' }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
I receive the following error:
undefined method `calluser_company' for #<CompaniesController:0x007fdd893f3270>
Any Idea why this is happening ? I will appreciate any help.
I read a previous similar problems but they didn't work for me:
Create a new action for existing controller
Route a form to new controller action in Ruby on Rails
Your routes describes:
call_user_company GET /companies/:id/call_user(.:format) companies#call_user
so you can use call_user_company_url(#company) or call_user_company_path(#company) instead of calluser_company(#company)
Related
I have a Rails 3.2 app that uses session store in the controllers to get the user back to the screen they were previously on.
It's been working fine for over a year. All of a sudden, the production version on Heroku, has started having issues. The user is looking at a worequest and clicks on the following in order to add a comment.
<%= link_to 'New Comment', new_comment_path(:worequest_id => #worequest.id), :class => 'btn btn-primary' %>
This is the code I use:
def new
#comment = Comment.new
#comment.build_attachment
#worequest = params[:worequest_id]
session[:return_to] ||= request.referer
respond_to do |format|
format.html # new.html.erb
format.json { render json: #comment }
end
end
# POST /comments
# POST /comments.json
def create
#comment = Comment.new(params[:comment])
respond_to do |format|
if #comment.save
if session[:return_to] != nil
format.html { redirect_to session.delete(:return_to), notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
else
format.html { redirect_to :back, notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
end
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
In development and staging (on Heroku), the user goes back to the worequest after entering a new comment.
Now in Production, the url looks like this:
mywebsite/comments instead of mywebsite/worequests/639
I'm not even sure where the session[:return_to] gets stored. Therefore, I'm having trouble debugging the issue.
Thanks for your help!!
Are you able to replicate this behavior 100% of the time, or just seeing it sometimes in your log?
It looks like someone is getting to comments#new from /comments (comments#index). Is there a route to /comments?
Run rake routes to see all your routes.
If there is a route to comments#index, and there's no reason for it to be exposed because you only intend for people to post comments from within the context of a specific article, consider removing it the comments#index route.
Also, one thing to consider, request.referrer is not always available. It's sent by the client who may choose not to send it (e.g. certain privacy extensions remove this header).
I'm trying to automatically pull ticker data from the Bitfinex bitcoin exchange and put it into a database for a Ruby on Rails project. Right now, I've got a controller BitfinexesController that does pull the data and create a new row on the corresponding database table bitfinexes when the method self.create is called. Right now I'm just trying to write a script that will call that method on a loop. I've tried to do this in the view but keep getting told that that there is an "undefined method 'create' for #".
Am I thinking about this wrong? I've just started learning Ruby on Rails.
Here's the method:
def self.create
newdata ="run script that grabs data"
#bitfinex = Bitfinex.new(eval(newdata))
respond_to do |format|
if #bitfinex.save
format.html { redirect_to #bitfinex, notice: 'Bitfinex was successfully created.' }
format.json { render :show, status: :created, location: #bitfinex }
else
format.html { render :new }
format.json { render json: #bitfinex.errors, status: :unprocessable_entity }
end
end
end`
I am trying to use the standard create method created for Ruby/Rails projects and simply pass in an additional form field that tells the method how many objects to create (vs just creating one object). The standard create method looks like so:
def create
#micropost = Micropost.new(micropost_params)
respond_to do |format|
if #micropost.save
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
end
I want to pass in an additional data (form field called number_to_create) which tells the method how many of the microposts to create. I just added a new form field like this, in addition to the other micropost form field params:
<%= text_field_tag :number_to_create %>
My question is how do I modify the create method code such that it creates N number of micropost objects vs. just one. So if I pass in 3 from the form along with the other micropost attributes, the method creates 3 identical micropost objects, not just one as it currently does.
Thanks in advance for your help on this.
You could use the param as times
#microposts = Micropost.transaction do
[].tap do |microposts|
param[:number_to_create].times do
microposts << Micropost.create(micropost_params)
end
end
end
respond_to do |format|
if #microposts.all? &:persisted?
format.html { redirect_to #micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: #micropost }
else
format.html { render :new }
format.json { render json: #micropost.errors, status: :unprocessable_entity }
end
end
The transaction block is to make sure that either all of them gets saved, or none of them gets saved, this way you can fix your errors and recreate them without worrying of getting any stray saved objects
from the default scaffold generator I have the following create action in my blogs controller:
# POST /blogs
# POST /blogs.json
def create
#blog = Blog.new(params[:blog])
respond_to do |format|
if #blog.save
format.html { redirect_to #blog, notice: 'Blog was successfully created.' }
format.json { render json: #blog, status: :created, location: #blog }
else
format.html { render action: "new" }
format.json { render json: #blog.errors, status: :unprocessable_entity }
end
end
end
When the sent form contains errors, my browser is redirected to /blogs URL but in the page the new action is rendered.
This is really ugly in my opinion and (also to simplify my javascript) I would like the browser to remain in the same blogs/new URL.
I tried with changing redirect_to :new instead of render action: "new", but this of course loses the #blog data.
any clue on how to do this?
thanks,
If you want to keep new in your path you could redirect with params like so:
redirect_to new_blog_path(blog: params[:blog])
and then check for these params in blog#new
How do I add a route to a custom controller methods in Rails?
I have the following methods and want to call them via AJAX.
def w_destroy
render json: RHoliday.where(holiday_id: params[:holiday_id].to_s, group_id: params[:group_id].to_s).destroy
end
def w_create
#r_holiday = RHoliday.new(r_holiday_params)
respond_to do |format|
if #r_holiday.save
format.html { redirect_to #r_holiday, notice: 'RHoliday was successfully created.' }
format.json { render action: 'show', status: :created, location: #r_holiday }
else
format.html { render action: 'new' } format.json { render json: #r_holiday.errors, status: :unprocessable_entity }
end
end
end
The question is:
How do I do this? I think I need to edit my routes.rb but I don't know what to add there. I just used resources :r_holidays, but this just creates the default routes.
You can add extra routes out of resources scope in many different ways:
#config/routes.rb
resources :r_holidays do
post :w_create
delete :w_delete
end
Specifically, the resources call basically gives you Rails' standard 7 RESTful routes - you can use a code block to define extra routes as required. You should read the documentation to get some more ideas