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
Related
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.
I have a Project that belongs to User. In my user view I have a link to add a new project, with the parameter for the user I want to add the project to.
<%= link_to 'Add new project', :controller => "project", :action => "new", :id => #user %>
Url: /projects/new?id=62
Adding a project to a user works. The problem is when the validation fails while adding a new project and I do a render.
def create
#project = Project.new(params[:project])
if #project.save
redirect_to :action => "show", :id => #project.id
else
render :action => "new"
end
end
view:
<%= form_for #project do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.hidden_field :user_id , :value => params[:id] %>
<%= f.submit "Create project" %>
<% end %>
routes
resources :users do
resources :projects
end
How can I keep the parameter for the user after the render? Or is there some better way to do this? Been looking at a lot of similar questions but can't get it to work.
try
render :action => "new", :id => #project.id
if its not works for you, then try alternate way to pass the parameter to your render action.
This can also help you->
Rails 3 Render => New with Parameter
You shouldn't use params[:id] to assign value to this form field. Instead, add this to your #new action in controller:
def new
#project = Project.new(user_id: params[:id])
end
and then just write this in your form:
<%= f.hidden_field :user_id %>
Because #project was defined in your #new and #create actions and because it already contains a Project instance with a user_id assigned to it, the value would automatically be added to this field.
Having a bit of trouble with the following ruby on rails code snippet - I have books and would like to update a book's name, here is how I try to do it:
<h1>Edit the book "<%= #book.name %>"</h1>
<%= form_tag action: :update, id: #book do %>
<p>
<label for="book_name">Name</label>
<%= text_field :book, :name %>
<%= submit_tag 'Save changes' %>
</p>
<% end %>
This is what I do in the books controller:
def edit
#book = Book.find(params[:id])
end
def update
#book = Book.find(params[:id])
if #book.update_attributes(params[:book])
redirect_to :action => 'show', id => #book
else
#subjects = Subject.find(:all)
render :action => 'edit'
end
end
These are my routes:
root to: 'authors#index'
resources :books, :authors
When I click the submit button in the form, it gives me No route matches [POST] "/books/5" and directs to localhost:3000/books/5 instead of staying and localhost:3000/books/5/edit
What am I doing wrong here? Shouldn't there be a put method for updating stuff somewhere rather than a post method?
Updates should use put not post.
<%= form_tag( { :controller => :books, :action => :update, :id => #book.id }, { :method => :put } ) do %>
or better yet use form_for
<%= form_for #book do |f| %>
On your console run "rake routes" and it will print out all available routes.
Please try this:
We need to specify match in routes file.
match "/books/:id" => "books#update"
resources :books should do the job. you dont have to explicitly use "match".
def edit
#book = Book.find(params[:id])
end
form.html
form_for #book, :method => :put do |f|
def update
#book = Book.find(params[:id])
#book.update_attributes(params[:book])
end
this should do the job.
I had this issue before. Everything was right but still getting the error then I found out it was
gem 'rails-api'
Removed it and it all worked fine.
In views/products/edit.html.erb I use:
<%= form_for(:product, :url => {:action => 'update', :id => #product.id}) do |f| %>
which generates:
<form method="post" action="/aircons/8" accept-charset="UTF-8">
and I get the following error:
The action '8' could not be found for ProductsController
when trying to update a product with id=8.
I think that form's method should be put. Is that right ? How should I fix this ?
Some controller code:
def edit
#product = Product.find(params[:id])
end
def update
update_params_with_new_values(params)
#product = Product.find(params[:id])
if #product.update_attributes(params[:product])
flash[:notice] = "Product updated successfully."
redirect_to(:product => 'index')
else
render('edit')
end
end
def update_params_with_new_values(params)
params[:product][:shop_id] = Shop.create(:name => params[:new_shop]).id if params[:product][:shop_id] == "new_shop"
params[:product][:brand_id] = Brand.create(:name => params[:new_brand]).id if params[:product][:brand_id] == "new_brand"
end
routes.rb contains only the following two lines:
root :to => "products#index"
resources :products
Why don't you just use:
<%= form_for #product do |f| %>
?
If it won't work, please add your routes to question.
try using this
<% form_for #product %>
#code goes here
<% end %>
you need not do all the things that you are trying. If you have created this Product model using scaffolding mechanism you must have its entry in the config/routes.rb file this will give you a path variable as below
GET /products/:id/edit edit return an HTML form for editing a photo
PUT /products/:id update update a specific photo
you can get the edit path as edit_product_path for more info on this have a look at this
Hope you understand it better now.
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.