actioncontroller

Rails ActionController::Live - Sends everything at once instead of async

假装没事ソ 提交于 2021-01-28 11:56:37
问题 I have an issue with rails ActionController::Live In the end I want to show the progress of FFMPEG to the user, but for now I want to get this minimal example running: Rails media_controller.rb: class MediaController < ApplicationController include ActionController::Live def stream puts "stream function loaded" response.headers['Content-Type'] = 'text/event-stream' i = 0 begin response.stream.write "data: 1\n\n" sleep 0.5 i += 1 puts "response... data: " + i.to_s end while i < 10 response

How can I access the data for the snake object sent through JSON in my params?

倾然丶 夕夏残阳落幕 提交于 2020-06-29 03:41:36
问题 Using javascript, I make a fetch post. const game = {name: this.player, snake: this.snake, score: this.score, apple: this.apple, skull: this.skull, completed: this.completed} return fetch("http://localhost:3000/games", { method: "POST", headers: { "Content-Type": "application/json"}, body: JSON.stringify(game) }) .then(resp => resp.json()) .then(json => Game.appendHighScores(json)); I access the data via params on the Ruby on Rails end. The data for params[:snake][:body] are supposed to look

Multiple update forms for one model

微笑、不失礼 提交于 2020-01-15 08:41:08
问题 I want to have multiple forms on one page. Let's make an example to understand what I want: I have a page for my admins, let's say it's the admins#show page. My admin has to change his name on one form on this page and on another form his age. I know I could create one form but I want to have multiple forms (because this is just an example). So my admins#show page looks something like this: <%= form_for @admin do |a| %> <%= a.label :name %> <%= a.text_field :name %> <%= a.submit "Submit name

How use data related to object which I'm not saving to database?

不想你离开。 提交于 2020-01-06 19:08:27
问题 I need to use User data, which he is entering in form, but not save it. I added attribute accessors into my User model: attr_accessible :paypal_email, :first_name, :last_name attr_accessor :first_name attr_accessor :last_name but how can I use it after user submits form? I need to verify account details, but didn't save them, so I need to use in controller @user.first_name and @user.last_name My verification action: def verify @user = User.find(params[:user_id]) require 'httpclient' require

Transaction Action with Ruby On Rails

谁说胖子不能爱 提交于 2020-01-02 04:44:05
问题 I have a complex action inside controller that performs several update queries to the database. How can I make this action acts like transaction without any structural refactoring? 回答1: MyModel.transaction do begin @model.update_stuff @sub_model.update_stuff @sub_sub_model.update_stuff rescue ActiveRecord::StatementInvalid # or whatever # rollback is automatic, but if you want to do something additional, # add it here end end Here are the docs for the transaction method. 回答2: It's posible to

how to execute an action if the before_action returns false

痴心易碎 提交于 2020-01-02 00:55:08
问题 I know that with the following code: before_action :signed_in?, only: [:new] the action new will be executed if the signed_in? returns true, but instead if I want the new action to be executed when signed_in? returns false what do I have to do? Do I have to create a new method called, for instance, not_signed_in? ? Here it is my signed_in? method def signed_in? !@current_user.nil? end 回答1: before_action doesn't work as you think - it doesn't prevent action to be executed if callback returns

How to render js template from module included in controller?

China☆狼群 提交于 2019-12-22 10:38:16
问题 I have an action in a controller concern, which gets included in a controller. This action does not render a js.erb file as specified under a respond_to block. How do I properly get an action in a controller concern to successfully render a js.erb file (or any view, for that matter)? Is it a problem with my routes? The link for the module action = link_to image_tag("upvote.png"), send("vote_socionics_#{votable_name}_path", votable, vote_type: "#{s.type_two_im_raw}"), id: "vote-#{s.type_two_im

Does Rails come with a “not authorized” exception?

巧了我就是萌 提交于 2019-12-20 11:03:32
问题 I am writing an application that uses plain old Ruby objects (POROs) to abstract authorization logic out of controllers. Currently, I have a custom exception class called NotAuthorized that I rescue_from at the controller level, but I was curious to know: Does Rails 4 already come with an exception to indicate that an action was not authorized? Am I reinventing the wheel by implementing this exception? Clarification : The raise AuthorizationException is not happening anywhere inside of a

How do I configure the hostname for Rails ActionMailer?

▼魔方 西西 提交于 2019-12-18 11:49:13
问题 I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer. If I use a normal link_to tag <%= link_to "click here", :controller => foo, :action => 'bar', :token => token %> I get a relative link - rather useless from an email. If I add in :only_path => false , then it errors saying I need to set default_url_options[:host]

default_url_options and rails 3

回眸只為那壹抹淺笑 提交于 2019-12-17 06:41:31
问题 As ActionController::Base#default_url_options is deprecated, I wonder how to set default url options in rails3. The default url options are not static but dependent of the current request. http://apidock.com/rails/ActionController/Base/default_url_options Thanks, Corin 回答1: To set url options for current request use something like this in your controller: class ApplicationController < ActionController::Base def url_options { :profile => current_profile }.merge(super) end end Now, :profile =>