I have a a custom post request as follows:
= form_for(#property, :html => {:id => "promo-upload-form"} , :url => url_for(:action => 'update_promo_image'),:method => :post) do |f|
The route is as follows:
post 'properties/update_promo_image', :as => 'update_promo_image'
It takes me to this action on controller:
def update_promo_image
#property = Property.find(params[:id])
if #property.update(property_params)
respond_to do |format|
format.html
format.js
end
else
render 'edit'
end
end
But I get the error:
Couldn't find Property without an ID
If I revert this back to the standard/default Update action it works perfectly.
Does anyone have any insight as to what I'm doing wrong please?
Thanks
Steve
Define your routes using resources and this should be easier:
routes.rb
resources :properties do
member do
post :update_promo_image
end
end
This will make an update_promo_image_property_url(object) route available.
View:
form_for(#property, :html => {:id => "promo-upload-form"} , :url => update_promo_image_property_url(#property)) do |form|
form.input :field1
form.submit
end
Which will route to your controller method (including params[:id] to identify the property) passing params[:property][:field1] for your logic. Your controller should be fine.
Related
I have a file reports/print.html.erb
in reports_controller
def print
#report = Report.find(params[:id])
respond_to do |format|
format.html { render :layout => false }
format.xml { render :xml => #report }
end
end
in routes.rb
match 'reports/print(:id)'
trying to call with
<%= link_to 'Print', report_print_path(:id => #report.id), :method => :put %>
and getting this error:
ActionController::RoutingError in Reports#show
No route matches {:action=>"print", :id=>23, :controller=>"report"}
Where am I going wrong?
Change your route to:
match 'reports/print/:id' => 'controller#print', :via => :put
That may fix it (didn't test the code though, and change the 'controller#print' part to your actual controller name.
made it work with
<%= link_to 'Print', print_url(:id => #report.id) %>
and
match 'print/(:id)' => 'reports#print', :via => :get, :as => :print
No idea why it was giving me problems, there's 4 hours of my life i'll never get back.
In routes.rb I have:
get "survey/show" => "survey#show"
post "survey/step_2" => "survey#step_2"
post "survey/step_3" => "survey#step_3"
And in step_2.html.erb I have:
<%= form_for #result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %>
And in survey_controller.rb I have:
def step_2
#result = Result.new(params[:result])
if #result.save
session[:result_id] = #result.id
render :action => "step_2"
else
render :action => "show"
end
end
def step_3
#result = Result.find(session[:result_id])
if #result.update_attributes(params[:result])
render :action => "step_3"
else
render :action => "step_2"
end
end
And when I submit the form on step_2 I get the following error:
No route matches "/survey/step_3"
I believe Rails form_for method may be making that a PUT request, since the #result object has an id. I believe you should change your form_for line to:
<%= form_for #result,
:url => { :controller => 'survey', :action => 'step_3' },
:html => { :method => :post} do |f| %>
or change the route type to put in routes.rb
You have to use match.
match 'survey/step_3' => 'survey#step_3', :via => 'post'
I might be wrong about the :via, but it's something like that.
in my group controller I have two methods:
def new
#group = Group.new
respond_to do |format|
format.js
end
end
def new_beta
#group = Group.new
respond_to do |format|
format.js
end
end
I have a form that starts like so:
<%= form_for Group.new, :remote => true do |f| %>
How can I get the form_for to post to the new_beta controller? Thanks
You can set :
<%= form_for Group.new, :url=>{ :action =>"new_beta", :controller =>
"group"}, :remote => true do |f| %>
(you can also -preferably- directly use a named route instead of ":url => ")
First this is bad practice but..
in your routes add
resources :groups do
member do
get :new_beta
post :new_beta_create
end
end
Now
<%= form_for Group.new, :url => new_beta_create_groups_path, :remote => true do |f| %>
However I recommend creating a new controller called something like: alternate_groups_controller. Even better make a namespace for them.
Good luck
This is what I have in my view:
<%= link_to_remote "Responded - Positive",
:url => contact_path(#contact, :status => 'positive response'),
:update => "status" %>
This is what I have as a route:
map.resources :contacts, :has_one => :status_contact
Here is what I used in my controller:
def create
#status_contact = StatusContact.new(params[:status_contact])
if #status_contact.save
#flash[:notice] = "Successfully created status contact."
#redirect_to #status_contact
render :text => "Set status to #{#status_contact.status}."
else
render :text => "bomb"
end
end
My desired outcome is that for the specific Contact, it will update the attribute Contact.status with the value 'positive response' and do so via ajax.
Right now, I am getting a 404 error. What do I need to do to correct this?
This is the error that I am still getting:
POST http://localhost:3000/contacts/24?method=put&status=positive+response 404 Not Found
312ms
You're probably sending your request with the wrong verb (:post instead of :put). Which action of your controller are you trying to reach? It's probably the update action… Try to modify your link by specifying the :put method:
<%= link_to_remote "Responded - Positive", :url => contact_path(#contact, :status => 'positive response'), :method => :put, :update => "status" %>
I have a model User that has_one user_profile and a User_Profile belongs_to user
in the User controller I have:
def personal
#user = User.find_by_id(params[:id])
#user_profile = #user.user_profile
#user_profile ||= #user.build_user_profile
end
def update_personal
#user = User.find_by_id(params[:id])
if #user.user_profile.update_attributes(params[:user_profile])
flash[:notice] = "OK"
redirect_to #user
else
flash[:notice] = "Fail"
render :action => 'update_personal'
end
end
In my personal.html.erb view I have:
<% semantic_form_for #user_profile, :url => { :action => "update_personal"} do |form| %>
<%= form.inputs %>
<%= form.buttons %>
<%end%>
And on the rountes I have:
map.resources :users, :member => {
:personal => :get,
:update_personal => :put
}
Now the strange thing is that I can do:
users/1/personal
to see the form but when I submit I get this error:
Unknown action
No action responded to 1.
It's trying to find an action with the name 1.
Can anyone point me out on the right direction?
I just got the problem again, and finally understood the problem and found the solution.
This time i was using an ajax call using getJason.
Because the call was a get, and in my routes I had a update_xxxxxx => :put,
the route was ignored and the default :controller/:action/:id was used.
I just had to put the update_xxxx => :get and the problem was solved.
Maybe this will help someone.