Using best in place to update a attribute in a Order form rails spit out undefined methodupdate_attributes' for #`
the form is not on a update action but in a sold action, so the form must be inside of a update action?
<%= best_in_place order, :track_number, :as => :input, :nil => "Track code", :url => sold_order_path(order)%>
Mark as:
<%= link_to "Sent", sent_order_path(order), :class => 'btn btn_small' %>
def sold
#q = Order.where('seller_id = ? and status = ?', current_vitrine.id, params[:status] || Order.statuses[0]).ransack(params[:q])
#orders = #q.result(distinct: true).paginate(page: params[:page], per_page: 22)
#order = Order.find_by_id(params[:id])
respond_to do |format|
if Order.where('seller_id = ? and status = ?', current_vitrine.id, params[:status] || Order.statuses[0]).update_attributes(params[:order])
format.html { redirect_to :back, notice: 'Comment was successfully updated.' }
format.json { respond_with_bip(#order) }
else
format.html { render action: 'sold' }
format.json { respond_with_bip(#order) }
end
end
end
Related
I want to update 2 column values in a Rails3 controller if a certain submit button is used on the form.
Submit button on form:
<%= simple_form_for #costproject, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
...
<%= f.submit 'Submit to Division', :class => 'btn btn-warning', :name => "submit1" %>
Update logic in Controller:
class CostprojectsController < ApplicationController
...
def update
...
params[:coststatus_id] = 2 if params[:submit1]
params[:submit_date] = Date.today if params[:submit1]
respond_to do |format|
if #costproject.update_attributes(params[:costproject])
flash[:success] = "Project Submitted" if #costproject.previous_changes.include?(:submit_date)
format.html { redirect_to nextpath }
format.json { render json: #costproject }
else
format.html { render action: "edit" }
format.json { render json: #costproject.errors, status: :unprocessable_entity }
end
end
end
If I stop execution with an alert, it looks like the params have been set. But, the values don't get saved to the db.
You need to update the model if you want it save to the DB. You're just changing params in your update action.
You need something like this:
def update
#cost_project = Costproject.find(params[:id])
#cost_project.update(coststatus_id: 2, submit_date: Date.today) if params[:submit1]
end
the .update will update and save the record in the DB
This seems to work:
params[:costproject][:coststatus_id] = 2 if params[:submit1]
params[:costproject][:submit_date] = Date.today if params[:submit1]
respond_to do |format|
def selected
#enquiries = Enquiry.all
#enquiry_details = EnquiryDetail.all
#comments = Comment.all
#enquiries.where("created_at >= ? AND created_at <= ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #enquiries and #enquiry_details and #comments }
end
end
How to call this method in my view.
<%= link_to '<button type="button">SELECT</button>'.html_safe, method: :selected %>
Create a route for the action selected and then do this;
<%= button_to "SELECT", { :controller => "CONTROLLER", :action => "selected"} %>
I am trying to update the attributes from an order within my shopping cart. To do so, I want to use in-place editing with the best_in_place gem ( https://github.com/bernat/best_in_place )
Anyhow, once it is being implemented it does not update the record on submit.
This is my view:
= form_for #order, :url => update_cart_path, :html => {:id => 'update-cart'} do |order_form|
%div#line_items_list
- #order.line_items.each do |line_item|
%section#line-item.columns.twelve
%div.line_picture.columns.three.alpha
%div#line_picture
= image_tag(line_item.variant.images.first.attachment.url(:product)) unless line_item.variant.images.length == 0
%div#line_description.nine.columns.omega
%span#line_title.nine.columns
%span.columns.alpha= full_product_name(line_item.variant)
%span#line_price.offset-by-five.columns.omega= money line_item.price
%div#optionsvariant
- if line_item.variant.option_values.any?
- line_item.variant.sorted_values.each do |ov|
%div#line_item_options
%span= ov.option_type.presentation
- if ov.swatch?
%span.imagelabel= image_tag("#{ov.swatch.url(:circle)}")
- else
%span.textlabel.not_selected= ov.presentation
%div#orderquantity
Quantity
= best_in_place [#order, line_item], :quantity, :type => :input
- if line_item.subscription_line?
%p#subline Auto ship: first pair ships today and every 1 month.
This is my controllers update aciton:
def update
#order = current_order
respond_to do |format|
if #order.update_attributes(params[:order])
format.html do
#order.line_items = #order.line_items.select {|li| li.quantity > 0 }
fire_event('spree.order.contents_changed')
respond_with(#order) { |f| f.html { redirect_to cart_path } }
end
format.json { respond_with_bip(#order)}
else
format.html {respond_with(#order)}
format.json {respond_with_bip(#order)}
end
end
end
How would it be the proper way to do so, so it updates the quantity of my Line_item in the cart?
Removing the
= form_for #order, :url => update_cart_path, :html => {:id => 'update-cart'} do |order_form|
from the top of the form solved the problem.
I have created a blog application using Ruby on Rails and have just added an authentication piece and it is working nicely. I am now trying to go back through my application to adjust the code such that it only shows information that is associated with a certain user.
Currently, Users has_many :posts and Posts has_many :comments.
When a post is created I am successfully inserting the user_id into the post table. Additionally I am successfully only displaying the posts that belong to a certain user upon their login in the /views/posts/index.html.erb view. My problem is with the comments.
For instance on the home page, when logged in, a user will see only posts that they have written, but comments from all users on all posts. Which is not what I want and need some direction in correcting. I want only to display the comments written on all of the logged in users posts.
Do I need to create associations such that comments also belong to user? Or is there a way to adjust my code to simply loop through post to display this data.
I have put the code for the PostsController, CommentsController, and /posts/index.html.erb below and also my view code but will post more if needed.
class PostsController < ApplicationController
before_filter :authenticate
auto_complete_for :tag, :tag_name
auto_complete_for :ugtag, :ugctag_name
def index
#tag_counts = Tag.count(:group => :tag_name,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
#ugtag_counts = Ugtag.count(:group => :ugctag_name,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
#vote_counts = Vote.count(:group => :post_title,
:order => 'count_all DESC', :limit => 20)
conditions, joins = {}, :votes
unless(params[:tag_name] || "").empty?
conditions = ["tags.tag_name = ? ", params[:tag_name]]
joins = [:tags, :votes]
end
#posts= current_user.posts.paginate(
:select => "posts.*, count(*) as vote_total",
:joins => joins,
:conditions=> conditions,
:group => "votes.post_id, posts.id ",
:order => "created_at DESC",
:page => params[:page], :per_page => 5)
#popular_posts=Post.paginate(
:select => "posts.*, count(*) as vote_total",
:joins => joins,
:conditions=> conditions,
:group => "votes.post_id, posts.id",
:order => "vote_total DESC",
:page => params[:page], :per_page => 3)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
format.json { render :json => #posts }
format.atom
end
end
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
def edit
#post = Post.find(params[:id])
end
def create
#post = current_user.posts.create(params[:post])
respond_to do |format|
if #post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(#post) }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(#post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
CommentsController
class CommentsController < ApplicationController
before_filter :authenticate, :except => [:show, :create]
def index
#comments = Comment.find(:all, :include => :post, :order => "created_at DESC").paginate :page => params[:page], :per_page => 5
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #comments }
format.json { render :json => #comments }
format.atom
end
end
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #comment }
end
end
# GET /posts/new
# GET /posts/new.xml
# GET /posts/1/edit
def edit
#comment = Comment.find(params[:id])
end
def update
#comment = Comment.find(params[:id])
respond_to do |format|
if #comment.update_attributes(params[:comment])
flash[:notice] = 'Comment was successfully updated.'
format.html { redirect_to(#comment) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #comment.errors, :status => :unprocessable_entity }
end
end
end
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(params[:comment])
respond_to do |format|
if #comment.save
flash[:notice] = "Thanks for adding this comment"
format.html { redirect_to #post }
format.js
else
flash[:notice] = "Make sure you include your name and a valid email address"
format.html { redirect_to #post }
end
end
end
def destroy
#comment = Comment.find(params[:id])
#comment.destroy
respond_to do |format|
format.html { redirect_to Post.find(params[:post_id]) }
format.js
end
end
end
View Code for Comments
<% Comment.find(:all, :order => 'created_at DESC', :limit => 3).each do |comment| -%>
<div id="side-bar-comments">
<p>
<div class="small"><%=h comment.name %> commented on:</div>
<div class="dark-grey"><%= link_to h(comment.post.title), comment.post %><br/></div>
<i><%=h truncate(comment.body, :length => 100) %></i><br/>
<div class="small"><i> <%= time_ago_in_words(comment.created_at) %> ago</i></div>
</p>
</div>
<% end -%>
I believe you can set up another relation on user model
has_many :comments, :through => :posts
and then user #user.comments.
First, don't mess with MVC. Line:
Comment.find(:all, :order => 'created_at DESC', :limit => 3)
should be in controller:
#comments = Comment.find(:all, :order => 'created_at DESC', :limit => 3)
and in view:
<% #comments.each do |comment| %>
Or even better with partials:
<%= render :partial => "comment_item", :collection => #comments %>
It will iterate over all #comments.
Next, if you want to display all comments assigned to a post, then relation post has_many comments is enough. Use it like this:
# controller
#posts = current_user.posts(:include => :comments)
# view
<% #posts.each do |post| %>
<%=h post.title %> - Comments: <br />
<% post.comments.each do |comment| %>
<%=h comment.body %>
<% end %>
<% end %>
If you want to show only comments posted by current_user then all your comments should have user_id field filled. And use it in the same way as you are showing users posts.
This is a continuation of Confused as to which Prototype helper to use. My code has been updated to reflect other user's suggestions:
(model) message.rb:
class Message < ActiveRecord::Base
after_create :destroy_old_messages
def old_messages
messages = Message.all(:order => 'updated_at DESC')
if messages.size >= 24
return messages[24..-1]
else
return []
end
end
protected # works without protected
def destroy_old_messages
messages = Message.all(:order => 'updated_at DESC')
messages[24..-1].each {|p| p.destroy } if messages.size >= 24
end
end
(view) index.html.erb:
<div id="messages">
<%= render :partial => #messages %>
</div>
<%= render :partial => "message_form" %>
(view) _message.html.erb:
<% div_for message do %>
<%= h message.created_at.strftime("%X") %> - <%= h message.author %><%= h message.message %>
<% end %>
(view) _message_form.html.erb:
<% remote_form_for :message, :url => { :action => "create" }, :html => { :id => 'message_form'} do |f| %>
<%= f.text_area :message, :size => "44x3" %><br />
<%= submit_to_remote 'submit_btn', 'submit', :url => { :action => 'create' } %><br />
<% end %>
(view) create.rjs:
page.insert_html :top, :messages, :partial => #message
page[#message].visual_effect :grow
page[:message_form].reset
flash[:notice]
flash.discard
# #old_messages.each do |m|
# page.remove(m.id)
# end
(controller) messages_controller.rb:
class MessagesController < ApplicationController
def index
#messages = Message.all(:order => "created_at DESC")
respond_to do |format|
format.html
format.js
end
end
def new
#message = Message.new
respond_to do |format|
format.html
end
end
def create
#message = Message.new(params[:message])
# #old_messages = Message.old_messages
respond_to do |format|
if #message.save
flash[:notice] = 'message created.'
format.html { redirect_to(messages_url) }
format.js
else
format.html { render :action => "new" }
end
end
end
def update
#message = Message.find(params[:id])
respond_to do |format|
if #message.update_attributes(params[:message])
flash[:notice] = 'message updated.'
format.html { redirect_to(messages_url) }
format.js
else
format.html { render :action => "edit" }
end
end
end
def destroy
#message = Message.find(params[:id])
#message.destroy
respond_to do |format|
format.html { redirect_to(messages_url) }
format.js
end
end
end
With the exception of the old_messages method in the model, all of the commented code were recommended changes from the previous post to make this work. But as soon as I uncomment the last three lines from create.rjs and #old_messages = Message.old_messages from the controller, I can't even submit messages with the message_form partial. Can anyone see what's wrong here? I'm just trying to create a basic app to help further my understanding of rails and rjs. I would greatly appreciate any suggestions or corrections you have to share, thank you for reading my post.
it's not what you're asking for, but i have a suggestion...
to get the older messages you can use named_scope.
in your case, (if i understood what you want) i think it would be something like:
# model
named_scope :limit, lambda { |num| { :limit => num } }
named_scope :order, lambda { |ord| { :order => ord } }
and
#controller
Message.order("updated_at DESC").limit(24)
the problem is that old_messages is an instance method, and you're calling from a class.
if you do
def self.old_messages
# ...
end
it's now a class method.
this blog has a good explanation about class and instance methods.