Question with nested route in Rails3 - ruby

there is my route:
namespace :admin do
resources :products do
resources :medias do
collection do
post :update_positions
end
end
end
end
and I write one form to create media:
<%= form_for(admin_product_medias_path(#product), :html => { :multipart => true }) do |form| %>
i find that the action of the generated form is "/admin/products/123213/medias/new"
I think the rule is wrong

<%= form_for #product, :url => admin_product_medias_path(#product), :html => { :method => :post, :multipart => true} do |f| %>
...
<% end %>

Related

Add extra options to link_to do

I would like to pass a parameter with this link_to helper. How do I do that?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
The original code I had used the standard text link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>
Just like in your second example:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
If you have a path helper for this (i.e. if it's something like PostsController's show action) then you could do:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>
From my understanding you want to pass options to the link_to helper when using a block.
From the documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
This is possible with the following format
link_to(options = {}, html_options = {}) do
# name
end
To give you an example and using the code you provided:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>

undefined method session_path when using devise

I am getting undefined method 'session_path' for #<#<Class:0x9859218>:0x8ebcbd0> when using devise with my custom controller and view, can anybody shed some light on what I have done wrong?
routes:
devise_for :users, :skip => [:sessions]
as :user do
get 'account/login' => 'account#login', :as => :new_user_session
post 'account/login' => 'account#login_user', :as => :user_session
delete 'account/logout' => 'account#logout', :as => :destroy_user_session
end
controller:
AccountController < Devise::SessionsController
def login
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
respond_with(resource, serialize_options(resource))
end
def login_user
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_flashing_format?
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
def logout
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
set_flash_message :notice, :signed_out if signed_out && is_flashing_format?
yield if block_given?
respond_to_on_destroy
end
view:
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<ul>
<li>
<%= f.label :login, "Email", :class => labels %>
<%= f.email_field :login, :autofocus => true, :class => "wideinput" %>
</li>
<li>
<%= f.label :password, :class => "labels" %>
<%= f.password_field :password, :autocomplete => "off" %>
</li>
<li class='button'>
<%= submit_tag "Login", :class => "btn1" %>
</li>
</ul>
<% end %>
Try to change this line
post 'account/login' => 'account#login_user', :as => :user_session
to
post 'account/login' => 'account#login_user', :as => :session

how to request a fields_for partial via ajax?

i have a nested form model
<%= simple_form_for(#product, :html => { :multipart => true }) do |f| %>
<%= f.input :name %>
#... and some other
<%= f.simple_fields_for :productgroups do |builder| %>
<%= render 'form_productgroup', { :f => builder } %>
<% end %>
<% end %>
## partial-level-1: form_productgroup
<%= f.input :extrapercentage %>
#... and some other
<%= f.simple_fields_for :productmaterials do |builder| %>
<%= render 'form_productmaterial', { :f => builder } %>
<% end %>
## partial-level-2: form_productmaterial
<%= f.input :price %>
#... and some other
=> everything fine serverside, but i need to add records via ajax because each productgroup or productmaterial belongs to a specific type
now the question:
how to create a controller action which returns the new material form?
This code returns an error that f is not existing:
def new_productgroup
#product = Product.find(params[:id])
#productgroup = Productgroup.new(:product_id => #product.id, :materialgroup_id => params[:materialgroup_id])
render "form_productgroup", { :f => ???? }
end
please help me!

how does a button_to in rails request a JSON response?

Im a rails noob, so bear with me.
I have the following in new.html.erb:
<h1>New page</h1>
<%= render 'pages/form' %>
<% if #page != 1 %>
<%= button_to 'New Page', pages_path %>
<%= button_to 'Done!', :action=> 'generate' %>
<% end %>
my controller looks like this
def generate
#presentation = current_presentation
respond_to do |format|
format.json { render json: #presentation.pages}
format.html {render :text => "html"}
end
end
I want the response to be JSON, but the code goes to format.html.
How do I instruct the button_to that my requested response is JSON?
Not sure if just one of them or both, but try :remote => true and data-type:
<%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
Using a path helper to build the path worked for me. eg.
<%= button_to "Create Widgit", widgits_path(format: 'json'), remote: true %>

Rails 3 - Category Filter Using Select - Load Partial Via Ajax

I am trying to filter Client Comments by using a select and rendering it in a partial. Right now the partial loads #client.comments. I have a Category model with a Categorizations join. This all works, just need to know how to get the select to call the filter action and load the partial with ajax. Thanks for you help.
Categories controller:
def filter_category
#categories = Category.all
respond_to do |format|
format.js # filter.rjs
end
end
filter.js.erb:
page.replace_html 'client-note-inner',
:partial => 'comments',
:locals => { :com => Category.first.comments }
show.html.erb (clients)
<% form_tag(filter_category_path(:id), :method => :put, :class => 'categories', :remote => true, :controller => 'categoires', :action => 'filter') do %>
<label>Categories</label>
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id)) %>
<% end %>
<div class="client-note-inner">
<%= render :partial => 'comments', :locals => { :com => #comments } %>
</div><!--end client-note-inner-->
Hope that makes sense.
Cheers.
It's straightforward with a simple onchange
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id), onchange => 'form.submit()') %>

Resources