I am trying to figure out the new link_to in Rails 3 but i still don't get it
In Rails 2 I do:
<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>
but with the new syntax in Rails 3 how should it be?
I'm trying something like
<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>
but I'm not getting the quantity params in the controller's action
Something like this will send a "data" parameter with the value = 10.
link_to "My Link", { :controller => 'myctrler', :action=>"myact", :data=> 10 }, :remote=>true
I've never seen/used the :with option before. I'm sorry I can't help on that one.
They say its no longer supported in this answer on How to send Javascript Variable to a controller's action with the link_to helper?
you just need to add the variable with the value in the path url, for example:
<%= link_to "SEND DATA", "server_function?myData=10", remote: true %>
and if you need to send more than one parameter you must to use & for example ?myData=10&myOtherData=12, where the params are myData with the value of 10 and myOtherData with 12.
Related
When trying to use user authentication I get the following error: "NoMethodError in Viewer#show". And it addresses the error to <%= #page.body.html_safe%> in app/views/viewer/show.html.erb:1:in '_app_views_viewer_show_html_erb__685858346_34780128', which is only one line code by now.
But, when I call login page on browser address bar like: :3000/session/new, it comes Up. Which is not happening with :3000/session/destroy.
It seems that something related to the route is not working properly because, on the other hand, when I call a page on views/layouts/application.htm.erb like <li><%= link_to 'Home', {:controller => 'viewer', :action => 'show', :name => 'home'} %></li> it works, and if I switch to <li><%= link_to 'Home', view_page_path('home') %></li> it gives a similar error.
How can I solve that?
Your use of view_page_path('home') assumes that there is a named path view_page. Changing
get "/:name" => 'viewer#show'
to
get '/:name' => 'viewer#show', :as => :view_page
should fix that.
Secondly when using route helpers with named parameters you need to specify the name so Rails knows what parameters should be used. Change view_page_path('home') to view_page_path(:name => 'home').
And finally a NoMethodError for <%= #page.body.html_safe%> suggests to me that either #page or #page.body is nil.
I'm using simple_form with twitter bootstrap on Rails.
Everything works great, except when showing live validations in a form-inline class. My code for the form is:
<%= simple_form_for #message,
url: mailing_list_path,
html: { class: "form-inline" },
method: :post,
validate: true do |f| %>
<%= f.input_field :email_address, label: false %>
<%= f.submit "Submit" %>
<% end %>
This shows the error message properly (e.g. "is invalid"), but if I click off the input and then back on again, it adds another message (e.g. it would say "is invalid is invalid"). For example, two sequential invalid entries and then a blank entry would give:
Is there any way to have simple_form remove the existing error message before adding a new one?
EDIT:
I solved this using some ghetto js, but would still like to know if the functionality I mentioned above is built in. The divs are still there, they're just hidden instead of all showing together. Would be great to have them actually removed by the form validation...
$('input.email-address-input').on 'keyup', () ->
$(this).parent('form').siblings('.help-inline').hide()
Suddenly flash and params aren't working for me. Even my normal flash error/success messages don't work.
This is just one sample - I have the following in a view:
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
And in the new comment form:
<% if flash[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => flash[:worequest_id] %>
I have been using flash to pass data between a view and a form in quite a few places in my Rails app. Now, none of them are working!!
Could I have set some configuration that would turn off flash and params?
I appreciate your help!!
UPDATE
I read on another post, where someone was having "session problems" with FLASH. Where would I look for session problems?
UPDATE2
I added the following to the view that receives the data from the flash:
<% if flash[:worequest_id].blank? %>
<h3>flash blank</h3>
<% end %>
And "flash blank" showed up.
Really - This has been in many places of my app for months. And now it's not working!!!!!!!
UPDATE3
I also tried this:
<% flash.keep[:worequest_id] = #worequest.id %>
Is there a rails config file that deals with session? Maybe I messed up a parameter or something.
UDPATE4
This is the only line in my initializers/session_store.rb file:
Ndeavor::Application.config.session_store :cookie_store, key: '_ndeavor_session'
UPDATE5
Params don't work either !!!!!
params[:worequest_id]
UPDATE6
I just created a new app from scratch using Rails 3.2.11 and flash and params are working fine. So, it's not the version of Rails.
Im a bit confused about what/how you're trying to achieve.
Have you looked at the output produced by this?
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
I removed the jbuilder gem and now flash and params are working. It has to be something I did, because I just tried jbuilder in another app, and it didn't break flash. I'm going to try and figure out why my use of the gem caused the problem.
I currently have the following link:
<%= link_to 'New campus', new_campus_path(:university_id => #university.id) %>
and the url I get is:
/campus/new?university_id=1
How can I pass the parameter as POST?
I tried adding method => :post, but no luck there. I appreciate any help. Thanks.
The method parameter of link_to should work - what exactly was wrong with it?
<%= link_to 'New campus', new_campus_path(:university_id => #university.id), method: :post %>
Alternatively, you can try button_to
<%= button_to 'New campus', new_campus_path(:university_id => #university.id), method: :post %>
Edit: in both of these cases, the paremeter will still be added to the query string, but as long as the HTTP verb is correct, this shouldn't make a difference for rails, because access to both is done through the params hash.
If it's really important to not include the parameter, you can create the form yourself and add a hidden field:
<%= form_tag new_campus_path, method: :post %>
<%= hidden_field_tag :university_id, #university.id %>
<%= submit_tag 'New campus' %>
<% end %>
I don't think there's a shortcut for this in rails
Edit 2: As an aside, if I'm guessing correctly, this link is supposed to open a form for creating a new campus with a default university selected. If that's the case, you should really be using GET, because it's just a read action that doesn't cause any side effects.
You can use JQuery. Like this :
$("#my_link").click(function() {
$.ajax({
type: 'POST',
url: '/campus/new?university_id=1'
});
});
You can see the doc here : http://api.jquery.com/click/ and here : http://api.jquery.com/jQuery.post/.
I just need some clarity of thought on this one. I've got a Photos show page where a user can click "Like" to create a new like record for a particular photo.
On my photos show.html page I have the following form:
<%= form_for :like, :url => likes_path do |f| %>
<%= hidden_field_tag(:photo_id, #photo.id) %>
<%= f.submit "Like" %>
<% end %>
When I click the form I get a message that it "Couldn't find a Photo without an ID":
Started POST "/likes" for 127.0.0.1 at Sat Feb 25 16:43:21 -0500 2012
Processing by LikesController#create as HTML
Parameters: {"commit"=>"Like", "authenticity_token"=>"cQnuAHb/f6Sgo3aB5xPKErx3joQTV+DHGs0w9vi13vM=", "utf8"=>"\342\234\223", "photo_id"=>"47"}
Completed 404 Not Found in 68ms
ActiveRecord::RecordNotFound (Couldn't find Photo without an ID):
app/controllers/likes_controller.rb:8:in `create'
Not sure why when it seems to be passing in the photo_id correctly, no?
Here's a look at my likes_controller:
def create
#photo = Photo.find(params[:id])
#like = Like.new(:photo_id => :photo_id, :user_id => current_user.id)
end
Anyone see what I'm doing wrong here? Thanks.
It should work with Photo.find(params[:photo_id]).
The error message says that you called Photo.findwith a nil parameter, e.g. there is no parameter id in the request.