I am redirecting a user to a custom controller action like this:
<%= link_to url_for(:controller => :jobs, :action => :saved) do %>Saved Jobs<% end %>
it works fine, now i want to pass some value to action "saved".
Do you have any idea how to do this?
Try This
If you want to send the form directly by reloading the page as you are doing,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value") do %>Saved Jobs<% end %>
If you want to send it through ajax call from background, add :remote => true to the form,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value", :remote => true) do %>Saved Jobs<% end %>
Then in controller
#email = params[:email]
Related
i am using rails 2.3.11. I have a text_field_with_autocomplete thing used inside a form of model blog
<% remote_form_for :blog, :url => {:controller => "blogs", :action =>"add_blog"} do |f|%>
<%= f.text_field :title, :class => "admtxt1 wd1 lfloat" %>
<%= text_field_with_auto_complete "user_login", :login, { :size => 15}, { :url => {:controller => 'users', :action => 'autocomplete_for_blogowners'}, :method => :get, :param_name => 'term', :indicator => "spinner"} %>
<%= submit_tag "Save", :class => "btn-small margintop10" %>
<%end%>
In the blogs_controller,add_blog action i have
def add_blog
unless params[:blog].nil?
#blog = Blog.new(params[:blog])
#user = User.find_by_login(params["user_login"][:login])
#blog.owner = #user
end
respond_to do |format|
if #blog.save
#saved = true
format.js { render :action => 'add_blog' }
else
#saved = nil
format.js { render :action => 'add_blog' }
end
end
end
In my add_blog.js.rjs file
if #saved.nil?
page.replace_html 'message','<div id="msg" class = "textcolor3">' + #blog.errors.full_messages.join('<br />') + '</div>'
else
page.replace_html 'message','<div id="msg" class = "h3txt textcolor3"> Blog created successfully </div>'
end
My blog tablehas id,title,owner_id
And my model has the validation
validates_presence_of :owner_id, :title
The above code works perfectly with the validation of owner shouldnt be blank
But it didnt checks for the wrong user
For eg. if i am typing something where such a user doesn't exists .In that case if i am submitting the form with no results found in the box , Even tat time i am getting Owner shouldn't be blank.
How to check the existence of the User in this case and to add that in the error messages.
Your code is very strange. Why don't you use Rails 3?
Why don't you use REST and CRUD ?
Sorry but it is better to rewrite code absolutely.
It seems as php code, have you used php?
I have a model Product that is both used by a general controller products_controller and another one used for administration purposes products_controller in an /admin folder.
Because I wanted to create a form that will be sent via AJAX and depending on whether the product is there or not it will alter between a :create and an :update, I've decided to create a 3rd method that takes care of that; called insert.
def insert
#blah
end
The form tag I'm using for updating is:
<%= form_for(product, :url => admin_product_url(product), :remote => true, :layout => true) do |f| %>
So my question is how can I customize the form tag in order to call the insert action I tried:
<%= form_for(product, :url => admin_product_url(product), :action => :insert, :remote => true, :layout => true) do |f| %>
and
<%= form_for(product, :url => insert_admin_product_url(product), :remote => true, :layout => true) do |f| %>
so far with no luck.
I hope someone could show me the way to go.
Thanks
you should have to add below route in route file under the admin name space
match 'products/insert', :to =>'products#insert', :as => 'admin_products_insert'
Is it possible to submit another parameter outside the form data in rails? My problem is that I render different forms for different classes and send them to the same create method. I would like to send the class with the form (as value not as key in the hash).
Something like the :type parameter (that actually doesn't work)
<%= form_for(#an_object, :url => { :controller => :a_controller, :action => :create },
:type => #an_object.class.to_s.underscore) do |f| %>
The post message looks like:
{"commit"=>"Create Class of an Object",
"authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=",
"utf8"=>"✓",
"class_of_an_object"=>{"name"=>"a name",
"description"=>"a description"}}
and I would have a "type" => "class_of_an_object", but directly in the hash an not within the "class_of_an_object" hash.
<%= form_for #an_object,
:url => { :controller => :a_controller,
:action => :create,
:type => #an_object.class.to_s.underscore } do |f| %>
And I prefer to use named routes
<%= form_for #object, :url => object_path(#object, :type => "whtever"), :html => {:method => :post} do |f| %>
This works for me:
<%= form_for #foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>
I've got model with many associations.
User :has_many => :posts, :videos, :images etc
I've got helper method, which receives one of this association
draw_chart user.videos
def draw_chart(assoc)
assoc.each do |a|
link_to "update", [a.user, a], :method => :put, :remote => true, :confirm => 'orrly?'
end
end
Problem here is to send extra parameters to update action.
For example I want to get this url: /users/1/videos/2?status=done but I am confused how to realize this while I am using [a.user, a] instead of user_videos_path(video, :status => 'done'). But I can't use last, because it can be either videos, or images or anything else
I wont go using eval, how about this
<%= link_to('update', self.send("user_#{a.class.name.pluralize}_path", a, , :status => 'done'), :method => :put, :remote => true, :confirm => 'orrly?') %>
Only solution I can think up is this strange one:
link_to "update", eval("user_#{a.class.name.tableize}_path(#{a.school.id}, #{a.id}, :status => 'done')"), :method => :put, :remote => true, :confirm => 'orrly?'
but this looks messy
Hi i have the rails application. when i call the home controller i have index action . in the index.html.erb .i have some link_to_remote links.
<li><%=link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %></li>
<li><%=link_to_remote "Test",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html => { :id =>'cb1'},
:with => "'choose=' +encodeURIComponent('value')" %></li>
After clicking the "Example" and "Test" option the respective div got updated .... i want to highlighting the options after user click happened... consider If "Example" clicked i want to highlight "Example" option background...
I tried with current_page? rails helper method, :complete attribute of link_to_remote but no luck can any one please suggest me on this. ... Thanks in advance
there are params[:controller] and params[:action] that help you to know which action is current.
<div id='example_link'>
<%= link_to_remote "Example",
:update =>'view',
:url =>{:controller => 'home',:action => 'bank'},
:method => :post,
:html =>{:id =>"cb"},
:with => "'choose=' +encodeURIComponent('value')" %>
</div>
in your home controller
def bank
# your code
render :update do |page|
page << "$('#example_link').addClass('highlighted')"
end
end
in css
.highlighted{
background-color: yellow;
}