A user has the ability to edit their credit card information, like so:
CreditCardsController:
class CreditCardsController < ApplicationController
before_filter :authenticate_user!
respond_to :js
def edit
#cc = current_user.credit_cards.where(:id => params[:id]).first
respond_with #cc
end
end
_form.html.erb:
<%= form_for #cc, :remote => true, :html => { :method => :put } do |f| %>
<div id="cancel-subscription" class="modal-content">
<div class="header dotted-border">
<h2>Billing Information</h2>
<p>Edit the fields below to update your information</p>
</div>
<div class="content dotted-border">
<h2>Credit Card</h2>
</div>
</div>
<% end %>
For some reason the form_foris ignorning the :method option even if I leave it off. It keeps getting set to post. That is not correct since I am editing/updating a CC entry. Anyone else run into this issue?
Just adding
:html => { :method => put }
will not work, because form_for generate _methode hidden element with form on update and delete.
just using
:method => "put"
in form may work
Related
I got such a problem - there is a partial and I can not pass a variable there:
in partial I have;
<%= object.title %>
How I pass variables:
<%= render :partial => 'shared/post_preview', :locals => { :object => article } %>
the error I see looks like
**undefined local variable or method `object'**
Any ideas? I tried already everything seems...
also tried:
<%= render :partial => 'shared/post_preview', :object => article %>
<%= render 'shared/post_preview', :object => article %>
<%= render :partial => 'shared/post_preview', :object => article %>
everytime i see the same error...
Use this:
Assuming you have defined #article instance variable in action.
<%= render 'shared/post_preview', object: #article %>
This must solve your problem.
the problem was in the commented code in the partial file. Somehow it was counted like an actual code...
<!--
<div class="row">
<div class="col-lg-6">
<%= render :partial => 'shared/post_preview' %>
<%= render :partial => 'shared/post_preview' %>
</div>
<div class="col-lg-6">
<%= render :partial => 'shared/post_preview' %>
<%= render :partial => 'shared/post_preview' %>
</div>
</div>
-->
I am relatively new to programming and to rails so please be indulgent:)
I am building a website for myself which contains a blog. I have two models that are nested and I do not seem to understand how to use REST to perform certain actions on my articles and comments.
When I create a comment if the comment doesn't pass validation I want it to render the page again so that the user can correct his mistakes and resubmit the comment. When I try to render, it gives me a missing template error.
Here is the code:
You can also find this code on github --> https://github.com/MariusLucianPop/mariuslp-
routes.rb
Mariuslp::Application.routes.draw do
get "categories/new"
root :to => "static_pages#index"
match "login" => "visitors#login" # not rest
match "logout" =>"visitors#logout" # not rest
match "comment" => "articles#show"
resources :articles do
resources :comments
end
resources :tags, :taggings, :visitors, :categories, :comments
end
articles_controller.rb
def show
#article = Article.find(params[:id])
#comment = #article.comments.new
end
comments_controller.rb
def create
article_id = params[:comment].delete(:article_id)
#comment = Comment.new(params[:comment])
#comment.article_id = article_id
if #comment.save
redirect_to article_path(#comment.article_id)
else
render article_path(#comment.article_id,#comment) ## This one doesn't work
end
end
def new
#comment = Comment.new
end
def destroy
Comment.find(params[:id]).destroy
redirect_to articles_path()
end
Views-articles:
_comment.html.erb
<div class="comment">
<%= comment.body %><br />
<%= link_to "Delete Comment", article_comment_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this comment?" %>
</div>
_comment_form.html.erb
<%= form_for #comment do |f|%>
<%= f.hidden_field :article_id%>
<%= f.label :body %><br />
<%= f.text_area :body, :cols => 50, :rows => 6 %><br />
<%= f.submit%>
<%end%>
show.html.erb
<p><%= link_to "<< Back to Articles", articles_path%></p>
<div class = "article_show">
<%= label_tag :category_id %>
<%= #article.category_id%> <br />
<%= label_tag :title%>:
<%= #article.title%> <br />
<%= label_tag :body%>:
<%= #article.body%> <br />
<%= label_tag :tag_list%>:
<%= #article.tag_list%><br />
</div>
<br />
<% if session[:username]== "marius"%>
<div class ="admin">
<%= link_to "Edit", edit_article_path(#article)%>
<%= link_to "Delete", article_path(#article), :method => :delete, :confirm => "Are you sure you want to delete this article ?"%>
</div>
<%end%>
<br />
<%= render :partial => 'comment', :collection => #article.comments %>
<%= render :partial => 'comment_form'%>
Have you tried to use where you point the problem?
render 'articles/show'
You don't need to use article_comment_path because that is a full path, not just the place where you store the view templates. In this case, you only need the view. Of couse you must be sure to get all instance variable which you use in this views.
UPDATE:
#article = Articles.find(article_id)
render 'articles/show'
I have a form as follows:
<%= form_for(:session, :url => sessions_path, :remote => true, :html => {:id => 'login_form'}) do |f| %>
<div class="formRow">
<%= f.label :email %><br>
<%= f.text_field :email, :value => (#email if #email) %>
</div>
<div class="formRow">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="formRow small">
<%= link_to "I forgot my password",'#' %>
</div>
<div class="formRow">
<%= f.submit signin_button_text, :class => "button-big left" %>
</div>
<% end %>
It goes to this controller:
def create
#email = params[:session][:email]
user = User.authenticate(params[:session][:email],params[:session][:password])
respond_to do |format|
if user.nil?
#title = "Sign in"
flash.now[:error] = "Invalid email/password combination"
format.js {render :action => :new }
else
sign_in user
format.js {render :action => :create }
end
end
end
Here is the new.js file:
$('#login_form').replaceWith("<%=escape_javascript(render 'login_form')%>");
if($('.flash-block').length ==0) {
$('#login_form').before("<div class='flash-block error'><span><%=escape_javascript(flash[:error])%></span></div>");
}
For some reason if the form is submitted with errors it loops four times.
I don't understand why.
Is there something in the code that causes this to loop?
I am assuming that you are using jquery. This is usually happened when there is an incomplete call or there is some sort of error and you haven't refresh the page. Try something like this:
<script type="text/javascript">
$('#login_form').submit(function() {
$(this).unbind('submit').submit();
});
</script>
I was using Fancybox and was not opening it in an iframe. So I wound up loading the jquery libraries twice and thus when I submitted the form I had multiple submissions.
Once I opened Fancybox in an iframe it submitted only once.
Using this previous question as a guide, I've attempted to create a ul navigation header above a container that renders partials within the container when clicked. (Hopefully that makes some sense, but it may not be important.) Until the links for the partials are clicked, I have it rendering a partial by default.
However, when I went to click my link_to in hopes of rendering the partial I get the following error:
uninitialized constant ProfileController
I'm using Rails 3. Here's my relevant code:
ProfilesController:
def show_about
#is_on_show_about = true
end
def show_info
#is_on_show_info = true
end
views/profiles/show.html.erb:
<div id="info">
<div id="infoContainer">
<% if #is_on_show_about %>
<%= render :partial => 'show_about' %>
<% elsif #is_on_show_info %>
<%= render :partial => 'show_info' %>
<% end %>
<ul id="info">
<li>
<%= link_to 'About', show_about_path, :remote => true %>
</li>
</ul>
<ul id="settingsLinks">
<li>Advice</li>
<li>
<%= link_to 'Info', show_info_path, :remote => true %>
</li>
</ul>
</div>
<%= render :partial => 'show_about' %>
Routes.rb:
map.show_info 'profiles/:id/info', :controller => 'profile', :action => 'show_info'
map.show_about 'profiles/:id/about', :controller => 'profile', :action => 'show_about'
Can anyone help me fix this and explain what went wrong?
Both of your routes are incorrect.
If your controller is indeed named ProfilesController (plural) then your routes should use :controller => 'profiles', instead of :controller => 'profile'.
I would like to ask, if is some rule for action, where is the form send... If I have a in controller
def edit
#shop = Shop.find(params[:id])
end
View:
<%= form_for(#shop, :html => {:multipart => true}) do |f| %>
So in HTML source is:
<form accept-charset="UTF-8" action="/shops/11" class="edit_shop" enctype="multipart/form-data" id="edit_shop_11" method="post">
And this mean, this form will be send on :controller => 'shops', :action => 'update'.
And I am trying to change this action, where will be form send. I tried something like this:
<%= form_for(#shop, :url => {:controller => 'shops', :id => params[:id], :action => 'aupdate'}, :html => {:multipart => true}) do |f| %>
But in HTML source is
<form accept-charset="UTF-8" action="/shops/aupdate?id=11" class="edit_shop" enctype="multipart/form-data" id="edit_shop_11" method="post">
I would like to ask - how is possible, the form is not send on the action update in this case? And what would be needed to do for it?
Thank you
in your view:
<%= form_for :shop, :url => {:controller => 'shops', :action => 'aupdate', :id=> params[:id]}, :html=> {:id => "some-form-id", :multipart => true} do |f| %>
.......
<% end %>
in your routes:
resources :shops do
collection do
....
post :aupdate
....
end
end
in your controller:
def aupdate
....
puts "ssssssssssssssssssssssssssss",params.inspect
....
end